【发布时间】:2013-03-29 20:18:36
【问题描述】:
我正在实现一个函数,该函数通过调用deallocate_cache(void *ptr) 来解除分配给它的内存位置。
我手头任务的记忆结构如下:
22 typedef struct slab {
23 void *addr;
24 int bm[((SLAB_SIZE/8)/(8*sizeof(int)))+1]; // bitmap
25 struct slab *next;
26 } slab;
39 typedef struct {
40 int alloc_unit;
41 slab S;
42 } cache;
45 typedef struct { // structure for the entire memory
46 cache C[9];
47 region *R;
48 } memory;
因此,我有一个memory M,其中包含缓存c[0]、c[1]、...、c[8],而这些缓存又包含slabs。当一个slab通过分配填满时,我通过slab *next字段分配另一个作为链表元素。
为了让我的deallocate_cache(void *ptr) 正常工作,我必须首先找出ptr 是否在缓存范围内,如果是,在哪个缓存范围内。这是我到目前为止所拥有的:
1. // Check if ptr is in the range of (slab_addr, slab_size) for each slab in each cache
2. int ci = 0, counter, coefficient, done, freeable;
3. slab *look_ahead;
4. for(; ci < 9; ci++){
5. void *max_addr = &M.C[ci].S + SLAB_SIZE; // The upper bound of the address range of the first slab
6. counter = 1;
7. look_ahead = &M.C[ci].S;
8. while(look_ahead->next != NULL){
9. if( ptr > look_ahead->addr && ptr > max_addr){ // Check ptr is greater than S.addr. If yes, it's a good bet it's in this cache.
10. look_ahead = look_ahead->next;
11. max_addr += SLAB_SIZE; // Now the upper bound of the address range of the following slab
12. counter++; // slab counter, 1-based counting
13. }
14. else {
15. done = 1;
16. break;
17. }
18. }
19. if(done == 1) break;
20.
21. }
不幸的是,很明显,这并没有按预期工作。有什么方法可以使用这样的指针来比较地址,或者检查指针是否在给定的地址范围内?还是我必须简单地比较我知道分配给的最大范围内的每个地址?非常感谢任何帮助。
【问题讨论】:
-
我什么也不懂。如果你的slab是一个链表,为什么你需要9个不同的缓存?您是否要尽量减少每个平板中的元素数量?如果是这样,为什么 9 缓存?我不希望“max_addr += SLAB_SIZE”正常工作。链表不必在内存中继续(这就是你给它一个指针的原因)。按照你所做的,你可能想做:“max_addr = look_ahead->next + SLAB_SIZE”。这对我来说仍然很奇怪,但至少它遵循你之前所做的事情
-
我很抱歉,也许我可以更清楚。缓存服务于不同大小的
slabs:c[0] 只保存包含 8 字节分配的slab(每个slab最多8192,8192 等于SLAB_SIZE / M.C[0].alloc_unit),c[1] 保存包含16-字节分配等等。我会考虑你的建议,谢谢。
标签: c pointers void-pointers memory-management