📄 kalloc.c.svn-base
字号:
#define NULL 0x0#define BUF_NUM 9#define BUF_SZ_16 0 // 64 items (2K)#define BUF_SZ_32 1 // 64 items (2K)#define BUF_SZ_64 2 // 64 items (4K)#define BUF_SZ_128 3 // 32 items (4K)#define BUF_SZ_256 4 // 16 items (4K)#define BUF_SZ_512 5 // 8 items (4K)#define BUF_SZ_1024 6 // 8 items (8K)#define BUF_SZ_2048 7 // 8 items (16K)#define BUF_SZ_4096 8 // 8 items (32K)static struct malloc_freelist { unsigned int buf_sz; unsigned int num_items; unsigned char *startp; unsigned char *bufp; } malloc_freelist[BUF_NUM] = { 16 , 64, 0x0, 0x0, //BUF_SZ_16 32 , 64, 0x0, 0x0, //BUF_SZ_32 64 , 64, 0x0, 0x0, //BUF_SZ_64 128 , 32, 0x0, 0x0, //BUF_SZ_128 256 , 16, 0x0, 0x0, //BUF_SZ_256 512 , 68 , 0x0, 0x0, //BUF_SZ_512 1024, 24 , 0x0, 0x0, //BUF_SZ_1024 2048, 8 , 0x0, 0x0, //BUF_SZ_2048 4096, 16 , 0x0, 0x0 //BUF_SZ_4096 };static unsigned char malloc_buffer[156 * 1024];voidmalloc_init(){ unsigned int i, j; unsigned int tot_size = 0; unsigned char **bpp = (&malloc_freelist[0].bufp); for (i = 0; i < BUF_NUM; i++) { bpp = (&malloc_freelist[i].bufp); for (j = 0; j < malloc_freelist[i].num_items; j++) { *bpp = &malloc_buffer[tot_size]; bpp = &malloc_buffer[tot_size]; tot_size += malloc_freelist[i].buf_sz; } malloc_freelist[i].startp = malloc_freelist[i].bufp; }}char *kmalloc(unsigned int size, unsigned int flags){ unsigned int i; char *bufp; for (i = 0; i < BUF_NUM; i++) { if (size <= malloc_freelist[i].buf_sz) break; } if (malloc_freelist[i].num_items == 0) return NULL; // out of memory bufp = malloc_freelist[i].bufp; malloc_freelist[i].bufp = *((unsigned char **)bufp); malloc_freelist[i].num_items--; return bufp; }intkfree(unsigned char *bp){ unsigned int i; for (i = 0; i < BUF_NUM - 1; i++) { if (bp < malloc_freelist[i + 1].startp) break; } *((unsigned char **)bp) = malloc_freelist[i].bufp; malloc_freelist[i].bufp = bp; malloc_freelist[i].num_items++; return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -