0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
0000000 000000 000000 000000 000000 000000 000000 000000 000000
باستخدام ال mapped memory نقدر نعمل functions تدينا memory بالفعل initialized بدل ماتكون random memory زي malloc
وظيفة الداله مشابه جداااااااا ل calloc راجع ال man pages الخاصه بهم
header file :
كود PHP:
#ifndef __Malloc__
#define __Malloc__
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
void *MallocZero(size_t size);
void DestMalloc(void *addr , int length);
#endif
كود PHP:
#include "Malloc.h"
void *MallocZero(size_t size)
{
static int fd;
void *addr = NULL;
fd = -1;
if(fd == -1) /* first time to open /dev/zero*/
{
fd = open("/dev/zero" , O_RDONLY);
if(fd < 0)
return NULL;
}
addr = mmap(NULL , size , PROT_READ | PROT_WRITE , MAP_PRIVATE , fd , 0);
if(!addr)
return NULL;
return addr;
}
void DestMalloc(void *ptr , int length)
{
munmap(ptr , length);
}
كود PHP:
#include <stdio.h>
#include "Malloc.h"
int main()
{
int i;
char ch;
char *addr;
addr = MallocZero(52);
for(i = 0 , ch ='A' ; i<52 ; ch++)
{
addr[i++] = ch;
addr[i++] = ' ';
}
printf("%s\n",addr);
DestMalloc(addr , 52);
}

[email protected]:~/Desktop/linux.programming/calloczero# ./Mallcos
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
man mmap
MAP_PRIVATE
Create a private copy-on-write mapping. Updates to the mapping are not visible to
other processes mapping the same file, and are not carried through to the underlying
file. It is unspecified whether changes made to the file after the mmap() call are
visible in the mapped region.
Create a private copy-on-write mapping. Updates to the mapping are not visible to
other processes mapping the same file, and are not carried through to the underlying
file. It is unspecified whether changes made to the file after the mmap() call are
visible in the mapped region.
تعليق