The first two days encountered a requirement: the first address required for each application of a single memory space must be a multiple of 4
So the moment to think of this is not the application layer to malloc and free well, or you implement an application-level memory pool yourself to manage the memory space of the address allocation, and so on, and so on the implementation of many methods, The user's requirement is that as long as I apply to the space I can use and give me the return of the first address must be a multiple of 4, so the C language structure in the body, and the implementation of malloc and free, as well as the STL memory pool and other memory pool to achieve the way we can achieve this need reference.
Long thinking and finally choosing the easiest way to do that is by encapsulating malloc and free so we need to consider the main problem:
1. How to ensure that the application memory is a multiple of 4
So we thought we could find the memory address of a multiple of the nearest 4 of the memory address we're applying to, as we return to the upper level using this interface. The current address is 0 and we return directly to 1 and return to 1+3=4 2 to return to 2+2=4, and so on. But some of the memory in the middle is not used And the user gets not enough memory space, so we want to apply for memory space based on the user to apply for more than 4 space, so through the extra 4 space to ensure that users can get the address is a multiple of 4.
2. How to Release
So the current malloc and free address is the address that you actually get, and the address we give to the user is part of the real application space, and this address can result in 4 bytes of memory leaking if passed directly to free. So we thought about the size of the space that was recorded in one place when free was releasing space, how do you know how to release the free, hahaha, so we thought of the beginning we will be the address of the offset to record, in the release time again based on the offset to find the real release of the address.
The following is my implementation of the code, would like to paint the explanation, but recently busy, time is urgent, record, I hope that other people have a better way to achieve a lot of communication Kazakhstan.
/************************************************************************* > File name:malloc_free.c > Author : Weeks > Mail:18402927708@163.com > Created time:sun Dec 2017 01:53:56 PM CST ***************************** /#include <stdio.h> #include <stdlib.h> #define NUM 4 void* My_
malloc (size_t size) {if (size = = 0) {return NULL;
} size_t real_size = size + NUM;
void* reall_addr = malloc (real_size);
if (reall_addr = = null) {return null;
} void* return_addr = reall_addr + (num-((size_t) reall_addr% num));
unsigned char *record = (unsigned char*) return_addr-1);
*record = (unsigned char) (num-(size_t) reall_addr% num));
printf ("reall_addr:0x%x\n", reall_addr);
printf ("return_addr:0x%x\n", return_addr);
printf ("Record:%u\n", *record);
return return_addr;
} void My_free (void *addr) {if (addr = NULL) {return;
}unsigned char record = * ((unsigned char*) addr-1);
printf ("Record:%u\n", record);
void* reall_addr = (void*) ((unsigned char*) addr-record);
printf ("reall_addr:0x%x\n", reall_addr);
Free (REALL_ADDR);
#define SIZE int main () {void *p = My_malloc (size);
unsigned char* arr = (unsigned char*) p;
unsigned char i=0;
for (; i<size; i++) {Arr[i] = i;
printf ("arr[%u]=%u", I,arr[i]);
printf ("\ n");
My_free (P);
return 0;
}