user-0 提问: 动态内存分配算法(最简单实现)

回复:
user: user-0
content: 引用 1 楼 max_min_ 的回复:void *realloc(void *ptr, size_t size); //当然这个有可能首地址发生变化的！  malloc的源码也是通过sbrk, brk 这些底层函数来的，你可以借鉴下！  直接调用内核函数不行，目前面对的是嵌入式环境，可以简单理解为 pMemStart 指向的内存内核是操作不了的。 正在学习malloc函数的实现，但最好还是能简单点。

user: user-1
content: void *realloc(void *ptr, size_t size); //当然这个有可能首地址发生变化的！  malloc的源码也是通过sbrk, brk 这些底层函数来的，你可以借鉴下！

user: user-2
content: 就搞个内存池吧，如果只是分配 同一种对象的话.

user: user-3
content: 动态内存分配最常用就两种基本算法: 边界标识法和伙伴系统  看《数据结构》（C语言版）严蔚敏 第8章 动态存储管理

user: user-4
content: 我觉得我这个应该算是最简单的了  #ifndef __MY_MALLOC__H_ #define __MY_MALLOC__H_  /**/ void   malloc_init(); void * my_malloc(int numbytes); void   my_free(void *firstbyte); void   Printf();  /**/ struct mem_control_block{   int is_available;   int size; };  #endif   #include "my_malloc.h"  /**/ static int has_initialized = 0; static void *managed_memory_start; static void *last_valid_address; void Printf() {    printf("%p\n",last_valid_address); } void malloc_init()  {      last_valid_address = (void *)sbrk(0);     managed_memory_start = last_valid_address;    has_initialized = 1;  }   void my_free(void *firstbyte) {    struct mem_control_block *mcb;    mcb = firstbyte - sizeof(struct mem_control_block);     mcb->is_available = 1;  }  void * my_malloc(int numbytes) {    void *  current_location;    void *  memory_location;    struct  mem_control_block *current_location_mcb;     if ( !has_initialized) malloc_init();     current_location = managed_memory_start;        numbytes += sizeof(struct  mem_control_block);     while( current_location < last_valid_address)    {         current_location_mcb = (  struct  mem_control_block * )current_location;                 if(current_location_mcb->is_available && current_location_mcb->size >=numbytes)          {            current_location_mcb->is_available = 0;            memory_location = current_location + sizeof(struct  mem_control_block);            return memory_location;        }        current_location = current_location + current_location_mcb->size;    }         /**/       sbrk(numbytes);    current_location = last_valid_address;    last_valid_address += numbytes;    current_location_mcb = (  struct  mem_control_block * )current_location;    current_location_mcb->is_available = 0;    current_location_mcb->size = numbytes;    memory_location = current_location + sizeof(struct  mem_control_block);    return memory_location; }

user: user-5
content: #include <time.h> #include <stdlib.h> #include <stdio.h> typedef struct memory_block_t memory_block;  struct memory_block_t {   memory_block* next; };   memory_block* freeStroe;  void _init_() {    //puts(__FUNCTION__);    char theHeap[128 * 1024 * 1024]; //128M    memory_block* block;    freeStroe = (memory_block*)theHeap;    block = freeStroe;    while((char*)block + 128 < theHeap + 128 * 1024 * 1024)    {        block->next = (memory_block*)((char*)block + 128); //每128字节为一块 	   block = block->next;    }    block->next = 0;    }   //请求大小(0,128] void * _malloc_(unsigned int size) {   //puts(__FUNCTION__);   if(0 < size && size <= 128)   {      if(freeStroe) 	 { 	     void * ret = freeStroe; 		 freeStroe = freeStroe->next; 		 return ret; 	 }   }   return  0; } //p须是_malloc_返回的 void  _free_(void* p) {   //puts(__FUNCTION__);   if(p)   {     ((memory_block*)p)->next = freeStroe; 	freeStroe = ((memory_block*)p);   } }  void* tmp[1024]; int main() {    int i;    srand((unsigned int)time(NULL));    _init_();    for(i = 0; i < 1024; ++i)    {      tmp[i] =  _malloc_(rand() + 1);    }        for(i = 0; i < 1024; ++i)    {      _free_(tmp[i]);    } }   这个应该足够简陋了。

user: user-6
content: stack allocator算是最简单的了。

user: user-7
content: 参考Linux源代码中malloc和free的源代码。