[英]custom memory allocator
我寫了一個自定義的 memory 分配器。 它有 2 個我想刪除的限制,所以它像 malloc/free 一樣工作。
1.) mem_free 調用需要強制轉換為 unsigned char * 作為其輸入參數。 我希望它采用任何類型的指針。 如何才能做到這一點?
2.) 我寫的 memory 分配器將一塊 memory 分配到緩沖區的前面,它還寫入了它的大小。 空閑的 function 刪除緩沖區中分配的 memory 的最后一個塊。 所以 malloc/free 調用的順序很重要,否則它將不起作用。 我怎樣才能解除這個限制?
希望能夠做到這一點:
char* ptr1 = mem_alloc(10);
char* ptr2 = mem_alloc(4);
mem_free(ptr1);
mem_free(ptr2);
現在必須這樣做:
char* ptr1 = mem_alloc(10);
char* ptr2 = mem_alloc(4);
mem_free(ptr2);
mem_free(ptr1);
代碼:
unsigned char* mem_alloc(unsigned int size) {
unsigned int s;
if( (size + MEM_HEADER_SIZE) > (MEM_MAX_SIZE - mem_current_size_bytes) ) {
return NULL;
}
if(is_big_endian() == 0) {
s = (mem_buff[3] << 24) + (mem_buff[2] << 16) + (mem_buff[1] << 8) + mem_buff[0];
} else {
s = (mem_buff[0] << 24) + (mem_buff[1] << 16) + (mem_buff[2] << 8) + mem_buff[3];
}
memcpy(mem_buff + mem_current_size_bytes, &size, sizeof(unsigned int));
unsigned char* result = mem_buff + (mem_current_size_bytes + MEM_HEADER_SIZE);
mem_current_size_bytes += MEM_HEADER_SIZE + size;
return result;
}
void mem_free(unsigned char* ptr) {
unsigned int i,s;
for(i=0; i<mem_current_size_bytes; i++) {
if( (char*)ptr == (char*)(mem_buff + i) ) {
if(is_big_endian() == 0) {
s = (*(ptr - 1) << 24) + (*(ptr - 2) << 16) + (*(ptr - 3) << 8) + *(ptr - 4);
} else {
s = (*(ptr - 4) << 24) + (*(ptr - 3) << 16) + (*(ptr - 2) << 8) + *(ptr - 1);
}
mem_current_size_bytes-=s;
mem_current_size_bytes-=MEM_HEADER_SIZE;
break;
}
}
}
1)改用 void* 。
2) 存儲分配塊的地址 map 和未分配塊的單獨 map。 然后,您可以在已分配的 map 中查找正在釋放的塊,將其刪除,然后將該塊添加到未分配的 map(確保將其與任何一側的任何空閑塊合並)。 當然,這可以而且確實會導致 memory 碎片化,但這確實是不可避免的。
只需在 mem_free 的參數中使用 void* 指針而不是 char*。
為了使 memory 分配器與任何 memory 位置一起工作,您需要增加更多的復雜性...我建議研究如何 memory 堆進行管理,然后您會發現
你寫了你正在尋找想法,所以我附上了我在大學完成的一個項目,我們應該在其中實現 malloc() 和 free()......你可以看到我是如何做到這一點的,也許得到如果您願意,可以提供靈感或全部使用。 它肯定不是最快的實現,但相當容易並且應該沒有錯誤;-)
(請注意,如果來自同一課程的人碰巧遇到了這個——請不要使用這個,而是自己制作一個——因此是許可證;-)
/*
* The 1st project for Data Structures and Algorithms course of 2010
*
* The Faculty of Informatics and Information Technologies at
* The Slovak University of Technology, Bratislava, Slovakia
*
*
* Own implementation of stdlib's malloc() and free() functions
*
* Author: mnicky
*
*
* License: modified MIT License - see the section b) below
*
* Copyright (C) 2010 by mnicky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* a) The above copyright notice and this permission notice - including the
* section b) - shall be included in all copies or substantial portions
* of the Software.
*
* b) the Software WILL NOT BE USED IN ANY WORK DIRECTLY OR INDIRECTLY
* CONNECTED WITH The Faculty of Informatics and Information Technologies at
* The Slovak University of Technology, Bratislava, Slovakia
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdio.h>
typedef unsigned int MEMTYPE;
MEMTYPE *mem;
MEMTYPE memSize;
MEMTYPE avail; //1st index of the 1st free range
//return size of block
MEMTYPE blockSize(MEMTYPE x) {
return mem[x];
}
//return next free block
MEMTYPE next(MEMTYPE x) {
return mem[x + mem[x]];
}
//return index of pointer to next free block
MEMTYPE linkToNext(MEMTYPE x) {
return x + mem[x];
}
//initialize memory
void my_init(void *ptr, unsigned size) {
mem = (MEMTYPE *) ptr;
memSize = size / sizeof(MEMTYPE);
mem[0] = memSize - 1;
mem[memSize - 1] = memSize;
avail = 0;
}
//allocate memory
void *my_alloc(unsigned size) {
if (size == 0) { //return NULL pointer after attempt to allocate 0-length memory
return NULL;
}
MEMTYPE num = size / sizeof(MEMTYPE);
if (size % sizeof(MEMTYPE) > 0) num++;
MEMTYPE cur, prev; //pointer to (actually index of) current block, previous block
MEMTYPE isFirstFreeBeingAllocated = 1; //whether the first free block is being allocated
prev = cur = avail;
//testing, whether we have enough free space for allocation
test:
if (avail == memSize) { //if we are on the end of the memory
return NULL;
}
if (blockSize(cur) < num) { //if the size of free block is lower than requested
isFirstFreeBeingAllocated = 0;
prev = cur;
if (next(cur) == memSize) { //if not enough memory
return NULL;
}
else
cur = next(cur);
goto test;
}
if (blockSize(cur) == num) { //if the size of free block is equal to requested
if (isFirstFreeBeingAllocated)
avail = next(cur);
else
mem[linkToNext(prev)] = next(cur);
}
else { //if the size of free block is greater than requested
if (isFirstFreeBeingAllocated) {
if ((blockSize(cur) - num) == 1) //if there is only 1 free item left from this (previously) free block
avail = next(cur);
else
avail = cur + num + 1;
}
else {
if ((blockSize(cur) - num) == 1) //if there is only 1 free item left from this (previously) free block
mem[linkToNext(prev)] = next(cur);
else
mem[linkToNext(prev)] = cur + num + 1;
}
if ((blockSize(cur) - num) == 1) //if there is only 1 free item left from this (previously) free block
mem[cur] = num + 1;
else {
mem[cur + num + 1] = blockSize(cur) - num - 1;
mem[cur] = num;
}
}
return (void *) &(mem[cur+1]);
}
//free memory
void my_free(void *ptr) {
MEMTYPE toFree; //pointer to block (to free)
MEMTYPE cur, prev;
toFree = ((MEMTYPE *)ptr - (mem + 1));
if (toFree < avail) { //if block, that is being freed is before the first free block
if (((linkToNext(toFree) + 1) == avail) && avail < memSize) //if next free block is immediately after block that is being freed
mem[toFree] += (mem[avail] + 1); //defragmentation of free space
else
mem[linkToNext(toFree)] = avail;
avail = toFree;
}
else { //if block, that is being freed isn't before the first free block
prev = cur = avail;
while (cur < toFree) {
prev = cur;
cur = next(cur);
}
if ((linkToNext(prev) + 1) == toFree) { //if previous free block is immediately before block that is being freed
mem[prev] += (mem[toFree] + 1); //defragmentation of free space
if (((linkToNext(toFree) + 1) == cur) && cur < memSize) //if next free block is immediately after block that is being freed
mem[prev] += (mem[cur] + 1); //defragmentation of free space
else
mem[linkToNext(toFree)] = cur;
}
else {
mem[linkToNext(prev)] = toFree;
mem[linkToNext(toFree)] = cur;
}
}
}
這樣做是為了盡可能少地使用元信息空間,因此分配的空間由分配范圍的第一個節點中的數字標記,表示隨后分配的節點數。
空閑范圍內的空閑空間量由一個數字標記,表示后面的空閑節點(包括該節點)的數量,並且空閑范圍的最后一個節點包含下一個空閑范圍的第一個索引的數量 - 像這樣(紅色空間已分配,白色是空閑的):
它可以像這樣使用:
char region[30000000]; //space for memory allocation
my_init(region, 30000000); //memory initialization
var = (TYPE *) my_alloc(sizeof(TYPE)); //memory allocation
my_free((void *) var); //freeing the memory
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.