00001 #ifndef __MEMALLOC_H 00002 //2971148968181882 00003 #define __MEMALLOC_H 00004 00005 #include <stdint.h> 00006 #include <stdlib.h> 00007 #include <list.h> 00008 00009 /* 00010 * This header file describes the public interface of the first-fit 00011 * memory allocator. 00012 * 00013 * All functions except mem_init() must be implemented in a thread-safe 00014 * manner. Use the pthread_mutex_* functions as needed. 00015 */ 00016 00017 /* Initialize memory allocator to use 'length' 00018 bytes of memory at 'base'. */ 00019 void mem_init(uint8_t *base, size_t length); 00020 00021 /* Allocate 'length' bytes of memory. */ 00022 void * mem_alloc(size_t length); 00023 00024 /* Free memory pointed to by 'ptr'. */ 00025 void mem_free(void *ptr); 00026 00027 /* Return the number of elements in the free list. */ 00028 size_t mem_sizeof_free_list(void); 00029 00030 /* Dump the free list. Implementation of this method is optional. */ 00031 void mem_dump_free_list(void); 00032 00033 /* free_block and used_block describe the layout of a free and used 00034 * block of memory, respectively. 00035 * 00036 * Both block types have a length field. 00037 * The length describes the length of the block, including the 00038 * space taken up by the header. 00039 * 00040 * A free block also has a field that contains a list_elem so the block 00041 * can be inserted into the free list. 00042 * 00043 * Note that data is declared as an array of 0 bytes. 00044 * This allows you to use sizeof(struct used_block) to capture 00045 * the size of the header of each block of memory. 00046 * For more information on zero-length arrays, see 00047 * http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html 00048 * 00049 * Normally, this declaration would not be exported; 00050 * it would be in memalloc.c instead of here. 00051 * We provide it here as a suggestion. 00052 */ 00053 struct free_block 00054 { 00055 size_t length; /* length of block, including header */ 00056 struct list_elem elem; /* list element for free list */ 00057 }; 00058 00059 struct used_block 00060 { 00061 size_t length; /* length of block, including header */ 00062 uint8_t data[0]; /* memory_block.data points at the 00063 memory behind the length . */ 00064 }; 00065 00066 #endif /* __MEMALLOC_H */ 00067 00068 // vim: sw=2