j*****y 发帖数: 1071 | 1 void * mymalloc(int size, int align) // align is a power of 2
{
void * p = (void *)malloc(size + align - 1 + sizeof(void *));
void * p1 = (void *)((((int)p) + align -1 + sizeof(void *)) & (~(align
- 1)));
void **p2 = (void **)p1;
p2[-1] = p;
return p1;
}
void myfree(void *p)
{
void **p2 = (void **) p;
free(p2[-1]);
} |
|
j*****y 发帖数: 1071 | 2 void * mymalloc(int size, int align) // align is a power of 2
{
void * p = (void *)malloc(size + align - 1 + sizeof(void *));
void * p1 = (void *)((((int)p) + align -1 + sizeof(void *)) & (~(align
- 1)));
void **p2 = (void **)p1;
p2[-1] = p;
return p1;
}
void myfree(void *p)
{
void **p2 = (void **) p;
free(p2[-1]);
} |
|
g********o 发帖数: 30 | 3 问大家一道题,CISCO的。说是要实现一个myMalloc(),每次分配固定的size。固定
的size做起来应该简单不少。解法应该是用两个list,一个记录用过的,一个记录free
的,如果free的有,就把free的头结点去掉,加到used list的头部。假设可以用
malloc一次分配一个MAX_SIZE的大block,如何实现myMalloc和myFree?
这方法是我在事后想到的,不过我在如何初始化这个free list的时候卡住了。请问记
录free list如何初始化,是用n个结点的data来记录每一块内存的位置吗? |
|
r*******e 发帖数: 7583 | 4 固定size的相对容易
都不需要额外的list structure,直接在free memory block里做记录
对于每一个free block
在首地址处记录下一个free block的地址,形成一个embedded link list
初始状态:
全局变量FirstFreeBlock指向大block的首地址
对n = MAX_SIZE/size 个blocks,初始化其首地址使其形成链表
myMalloc操作:
rtn = FirstFreeBlock
FirstFreeBlock = FirstFreeBlock->next
return rtn
myFree:
addr->next = FirstFreeBlock
FirstFreeBlock = addr
free |
|
b******7 发帖数: 92 | 5 #define BLOCK_SIZE 1024
#define ALIGN(size,unit) (((size)+(unit-1))/(unit)*(unit))
struct ChunkHead{
ChunkHead * next;
};
#define CHUNK_SIZE ALIGN(sizeof(ChunkHead) + BLOCK_SIZE, sizeof(int))
ChunkHead * firstChunk;
void initChunk(void * addr, size_t size)
{
firstChunk = NULL;
Chunk * pre = NULL;
for(int i = 0; i < size/CHUNK_SIZE; i++)
{
ChunkHead * cur = (ChunkHead *)(addr + i* CHUNK_SIZE);
if( pre != NULL)
... 阅读全帖 |
|
a*******d 发帖数: 4846 | 6 In case your new year plans include formalizing your business ventures,
MyCorporation is offering their LLC formation and incorporation filing
services for free until 1/31/2010 with the coupon code MYFREE. You must
still pay shipping fees and the filing fees charged by each state.
http://www.mymoneyblog.com/archives/2010/01/free-incorporation-llc-
service-from-mycorporation.html?utm_source=feedburner&utm_medium=email |
|
d*a 发帖数: 1863 | 7 online taxes
myfree taxes 跟turbotax比起来哪个好用?
还是都差不多 |
|
|