Otherwise the returned pointers are not aligned either!
Török Edvin authored on 2010/03/23 01:36:03... | ... |
@@ -66,7 +66,6 @@ static inline void spam(const char *fmt, ...) { fmt = fmt; } /* gcc STFU */ |
66 | 66 |
#if SIZEOF_VOID_P==8 |
67 | 67 |
static const unsigned int fragsz[] = { |
68 | 68 |
/* SIZE PERM TEMP MAX ACT! */ |
69 |
- 8, /* Sparc SIGBUSes without this */ |
|
70 | 69 |
16, /* 1487281 7051 1487281 USE */ |
71 | 70 |
24, /* 89506 103 89510 USE */ |
72 | 71 |
32, /* 1313968 65 1313969 USE */ |
... | ... |
@@ -143,7 +142,6 @@ static const unsigned int fragsz[] = { |
143 | 143 |
|
144 | 144 |
static const unsigned int fragsz[] = { |
145 | 145 |
/* SIZE PERM TEMP ACT! */ |
146 |
- 8, /* Sparc SIGBUSes without this */ |
|
147 | 146 |
16, /* 1487589 7134 1487589 USE */ |
148 | 147 |
24, /* 116448 127 116452 USE */ |
149 | 148 |
32, /* 1287128 95 1287134 USE */ |
... | ... |
@@ -233,7 +231,10 @@ struct MPMAP { |
233 | 233 |
struct MP { |
234 | 234 |
unsigned int psize; |
235 | 235 |
struct FRAG *avail[FRAGSBITS]; |
236 |
- struct MPMAP mpm; |
|
236 |
+ union { |
|
237 |
+ struct MPMAP mpm; |
|
238 |
+ uint64_t dummy_align; |
|
239 |
+ } u; |
|
237 | 240 |
}; |
238 | 241 |
|
239 | 242 |
struct FRAG { |
... | ... |
@@ -275,20 +276,20 @@ struct MP *mpool_create() { |
275 | 275 |
memset(&mp, 0, sizeof(mp)); |
276 | 276 |
mp.psize = cli_getpagesize(); |
277 | 277 |
sz = align_to_pagesize(&mp, MIN_FRAGSIZE); |
278 |
- mp.mpm.usize = align_to_voidptr(sizeof(struct MPMAP)); |
|
279 |
- mp.mpm.size = sz - align_to_voidptr(sizeof(mp)); |
|
278 |
+ mp.u.mpm.usize = align_to_voidptr(sizeof(struct MPMAP)); |
|
279 |
+ mp.u.mpm.size = sz - align_to_voidptr(sizeof(mp)); |
|
280 | 280 |
if ((mpool_p = (struct MP *)mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE|ANONYMOUS_MAP, -1, 0)) == MAP_FAILED) |
281 | 281 |
return NULL; |
282 | 282 |
#ifdef CL_DEBUG |
283 | 283 |
memset(mpool_p, ALLOCPOISON, sz); |
284 | 284 |
#endif |
285 | 285 |
memcpy(mpool_p, &mp, sizeof(mp)); |
286 |
- spam("Map created @%p->%p - size %u out of %u - voidptr=%u\n", mpool_p, (char *)mpool_p + mp.mpm.size, mp.mpm.usize, mp.mpm.size, SIZEOF_VOID_P); |
|
286 |
+ spam("Map created @%p->%p - size %u out of %u - voidptr=%u\n", mpool_p, (char *)mpool_p + mp.u.mpm.size, mp.u.mpm.usize, mp.u.mpm.size, SIZEOF_VOID_P); |
|
287 | 287 |
return mpool_p; |
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
void mpool_destroy(struct MP *mp) { |
291 |
- struct MPMAP *mpm_next = mp->mpm.next, *mpm; |
|
291 |
+ struct MPMAP *mpm_next = mp->u.mpm.next, *mpm; |
|
292 | 292 |
unsigned int mpmsize; |
293 | 293 |
|
294 | 294 |
while((mpm = mpm_next)) { |
... | ... |
@@ -299,7 +300,7 @@ void mpool_destroy(struct MP *mp) { |
299 | 299 |
#endif |
300 | 300 |
munmap((void *)mpm, mpmsize); |
301 | 301 |
} |
302 |
- mpmsize = mp->mpm.size; |
|
302 |
+ mpmsize = mp->u.mpm.size; |
|
303 | 303 |
#ifdef CL_DEBUG |
304 | 304 |
memset(mp, FREEPOISON, mpmsize + align_to_voidptr(sizeof(*mp))); |
305 | 305 |
#endif |
... | ... |
@@ -309,7 +310,7 @@ void mpool_destroy(struct MP *mp) { |
309 | 309 |
|
310 | 310 |
void mpool_flush(struct MP *mp) { |
311 | 311 |
size_t used = 0, mused; |
312 |
- struct MPMAP *mpm_next = mp->mpm.next, *mpm; |
|
312 |
+ struct MPMAP *mpm_next = mp->u.mpm.next, *mpm; |
|
313 | 313 |
|
314 | 314 |
#ifdef EXIT_ON_FLUSH |
315 | 315 |
exit(0); |
... | ... |
@@ -328,15 +329,15 @@ void mpool_flush(struct MP *mp) { |
328 | 328 |
used += mpm->size; |
329 | 329 |
} |
330 | 330 |
|
331 |
- mused = align_to_pagesize(mp, mp->mpm.usize + align_to_voidptr(sizeof(*mp))); |
|
332 |
- if (mused < mp->mpm.size + align_to_voidptr(sizeof(*mp))) { |
|
331 |
+ mused = align_to_pagesize(mp, mp->u.mpm.usize + align_to_voidptr(sizeof(*mp))); |
|
332 |
+ if (mused < mp->u.mpm.size + align_to_voidptr(sizeof(*mp))) { |
|
333 | 333 |
#ifdef CL_DEBUG |
334 |
- memset((char *)mp + mused, FREEPOISON, mp->mpm.size + align_to_voidptr(sizeof(*mp)) - mused); |
|
334 |
+ memset((char *)mp + mused, FREEPOISON, mp->u.mpm.size + align_to_voidptr(sizeof(*mp)) - mused); |
|
335 | 335 |
#endif |
336 |
- munmap((char *)mp + mused, mp->mpm.size + align_to_voidptr(sizeof(*mp)) - mused); |
|
337 |
- mp->mpm.size = mused - align_to_voidptr(sizeof(*mp)); |
|
336 |
+ munmap((char *)mp + mused, mp->u.mpm.size + align_to_voidptr(sizeof(*mp)) - mused); |
|
337 |
+ mp->u.mpm.size = mused - align_to_voidptr(sizeof(*mp)); |
|
338 | 338 |
} |
339 |
- used += mp->mpm.size; |
|
339 |
+ used += mp->u.mpm.size; |
|
340 | 340 |
spam("Map flushed @%p, in use: %lu\n", mp, used); |
341 | 341 |
} |
342 | 342 |
|
... | ... |
@@ -352,7 +353,7 @@ int mpool_getstats(const struct cl_engine *eng, size_t *used, size_t *total) |
352 | 352 |
mp = eng->mempool; |
353 | 353 |
if (!mp) |
354 | 354 |
return -1; |
355 |
- for(mpm = &mp->mpm; mpm; mpm = mpm->next) { |
|
355 |
+ for(mpm = &mp->u.mpm; mpm; mpm = mpm->next) { |
|
356 | 356 |
sum_used += mpm->usize; |
357 | 357 |
sum_total += mpm->size; |
358 | 358 |
} |
... | ... |
@@ -365,7 +366,7 @@ void *mpool_malloc(struct MP *mp, size_t size) { |
365 | 365 |
unsigned int i, needed = align_to_voidptr(size + FRAG_OVERHEAD); |
366 | 366 |
const unsigned int sbits = to_bits(needed); |
367 | 367 |
struct FRAG *f = NULL; |
368 |
- struct MPMAP *mpm = &mp->mpm; |
|
368 |
+ struct MPMAP *mpm = &mp->u.mpm; |
|
369 | 369 |
|
370 | 370 |
/* check_all(mp); */ |
371 | 371 |
if (!size || sbits == FRAGSBITS) { |
... | ... |
@@ -422,8 +423,8 @@ void *mpool_malloc(struct MP *mp, size_t size) { |
422 | 422 |
#endif |
423 | 423 |
mpm->size = i; |
424 | 424 |
mpm->usize = needed + align_to_voidptr(sizeof(*mpm)); |
425 |
- mpm->next = mp->mpm.next; |
|
426 |
- mp->mpm.next = mpm; |
|
425 |
+ mpm->next = mp->u.mpm.next; |
|
426 |
+ mp->u.mpm.next = mpm; |
|
427 | 427 |
f = (struct FRAG *)((char *)mpm + align_to_voidptr(sizeof(*mpm))); |
428 | 428 |
spam("malloc @%p size %u (new map)\n", f, align_to_voidptr(size + FRAG_OVERHEAD)); |
429 | 429 |
f->u.sbits = sbits; |
... | ... |
@@ -579,7 +580,7 @@ uint16_t *cli_mpool_hex2ui(mpool_t *mp, const char *hex) { |
579 | 579 |
#ifdef DEBUGMPOOL |
580 | 580 |
void mpool_stats(struct MP *mp) { |
581 | 581 |
unsigned int i=0, ta=0, tu=0; |
582 |
- struct MPMAP *mpm = &mp->mpm; |
|
582 |
+ struct MPMAP *mpm = &mp->u.mpm; |
|
583 | 583 |
|
584 | 584 |
cli_warnmsg("MEMORY POOL STATISTICS\n map \tsize\tused\t%\n"); |
585 | 585 |
while(mpm) { |
... | ... |
@@ -593,7 +594,7 @@ void mpool_stats(struct MP *mp) { |
593 | 593 |
} |
594 | 594 |
|
595 | 595 |
void check_all(struct MP *mp) { |
596 |
- struct MPMAP *mpm = &mp->mpm; |
|
596 |
+ struct MPMAP *mpm = &mp->u.mpm; |
|
597 | 597 |
while(mpm) { |
598 | 598 |
volatile unsigned char *c = (unsigned char *)mpm; |
599 | 599 |
unsigned int len = mpm->size; |