[e70e45] | 1 | /******************************************************************* |
---|
| 2 | * File: omBinPage.c |
---|
| 3 | * Purpose: implementation of routines for primitve BinPage managment |
---|
| 4 | * Author: obachman (Olaf Bachmann) |
---|
| 5 | * Created: 11/99 |
---|
| 6 | *******************************************************************/ |
---|
[83342d] | 7 | #include <limits.h> |
---|
[be127e] | 8 | #include "omalloc.h" |
---|
[db143c] | 9 | |
---|
| 10 | #ifdef HAVE_OMALLOC |
---|
[be127e] | 11 | #include "omDefaultConfig.h" |
---|
[e70e45] | 12 | |
---|
| 13 | /******************************************************************* |
---|
[13fe1b] | 14 | * |
---|
| 15 | * Local declarations |
---|
| 16 | * |
---|
[e70e45] | 17 | *******************************************************************/ |
---|
| 18 | |
---|
| 19 | /* define if you want to keep regions approximately in order */ |
---|
| 20 | #define OM_KEEP_REGIONS_ORDER |
---|
| 21 | |
---|
| 22 | struct omBinPageRegion_s |
---|
| 23 | { |
---|
| 24 | void* current; /* linked list of free pages */ |
---|
| 25 | omBinPageRegion next; /* nex/prev pointer in ring of regions */ |
---|
| 26 | omBinPageRegion prev; |
---|
[13fe1b] | 27 | char* init_addr; /* pointer portion of inital chunk which is still free */ |
---|
| 28 | char* addr; /* addr returned by alloc */ |
---|
| 29 | int init_pages; /* number of pages still available in init_chunk */ |
---|
[384936] | 30 | int used_pages; /* number of used pages */ |
---|
[e70e45] | 31 | int pages; /* total size of region */ |
---|
| 32 | }; |
---|
| 33 | |
---|
| 34 | /* globale variable holding pointing int regions ring */ |
---|
| 35 | static omBinPageRegion om_CurrentBinPageRegion = NULL; |
---|
| 36 | unsigned long om_MaxBinPageIndex = 0; |
---|
| 37 | unsigned long om_MinBinPageIndex = ULONG_MAX; |
---|
| 38 | unsigned long *om_BinPageIndicies = NULL; |
---|
| 39 | |
---|
| 40 | /* declaration of local procs */ |
---|
| 41 | static void* omTakeOutConsecutivePages(omBinPageRegion region, int how_many); |
---|
| 42 | static omBinPageRegion omAllocNewBinPagesRegion(int min_pages); |
---|
| 43 | static void omFreeBinPagesRegion(omBinPageRegion region); |
---|
| 44 | |
---|
| 45 | static void omBinPageIndexFault(unsigned long low_index, unsigned long high_index); |
---|
| 46 | static void omRegisterBinPages(void* low_addr, int pages); |
---|
| 47 | static void omUnregisterBinPages(void* low_addr, int pages); |
---|
| 48 | |
---|
| 49 | OM_INLINE_LOCAL void omTakeOutRegion(omBinPageRegion region) |
---|
| 50 | { |
---|
| 51 | omAssume(region != NULL); |
---|
[13fe1b] | 52 | |
---|
[e70e45] | 53 | if (region->prev != NULL) |
---|
| 54 | { |
---|
| 55 | omAssume(region->prev != region && region->prev != region->next); |
---|
| 56 | region->prev->next = region->next; |
---|
| 57 | } |
---|
[13fe1b] | 58 | |
---|
| 59 | if (region->next != NULL) |
---|
[e70e45] | 60 | { |
---|
| 61 | omAssume(region->next != region && region->prev != region->next); |
---|
| 62 | region->next->prev = region->prev; |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | OM_INLINE_LOCAL void omInsertRegionAfter(omBinPageRegion insert, omBinPageRegion after) |
---|
| 67 | { |
---|
| 68 | omAssume(insert != NULL && after != NULL && insert != after); |
---|
| 69 | insert->next = after->next; |
---|
| 70 | insert->prev = after; |
---|
| 71 | after->next = insert; |
---|
| 72 | if (insert->next != NULL) |
---|
| 73 | { |
---|
| 74 | omAssume(insert->next != insert && insert->next != after); |
---|
| 75 | insert->next->prev = insert; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | OM_INLINE_LOCAL void omInsertRegionBefore(omBinPageRegion insert, omBinPageRegion before) |
---|
| 80 | { |
---|
| 81 | omAssume(insert != NULL && before != NULL && insert != before); |
---|
| 82 | insert->prev = before->prev; |
---|
| 83 | insert->next = before; |
---|
| 84 | before->prev = insert; |
---|
| 85 | if (insert->prev != NULL) |
---|
| 86 | insert->prev->next = insert; |
---|
| 87 | } |
---|
[13fe1b] | 88 | |
---|
[e70e45] | 89 | |
---|
| 90 | /******************************************************************* |
---|
[13fe1b] | 91 | * |
---|
[e70e45] | 92 | * Alloc/Free of BinPages |
---|
[13fe1b] | 93 | * |
---|
[e70e45] | 94 | *******************************************************************/ |
---|
| 95 | #define NEXT_PAGE(page) *((void**) page) |
---|
| 96 | #define OM_IS_EMPTY_REGION(region) ((region)->current == NULL && (region->init_addr == NULL)) |
---|
| 97 | |
---|
| 98 | omBinPage omAllocBinPage() |
---|
| 99 | { |
---|
| 100 | omBinPage bin_page; |
---|
| 101 | |
---|
| 102 | if (om_CurrentBinPageRegion == NULL) |
---|
| 103 | om_CurrentBinPageRegion = omAllocNewBinPagesRegion(1); |
---|
| 104 | |
---|
| 105 | while (1) |
---|
| 106 | { |
---|
| 107 | if (om_CurrentBinPageRegion->current != NULL) |
---|
| 108 | { |
---|
| 109 | bin_page = om_CurrentBinPageRegion->current; |
---|
| 110 | om_CurrentBinPageRegion->current = NEXT_PAGE(bin_page); |
---|
| 111 | goto Found; |
---|
| 112 | } |
---|
[ff04713] | 113 | if (om_CurrentBinPageRegion->init_pages > 0) |
---|
[e70e45] | 114 | { |
---|
[13fe1b] | 115 | bin_page = (omBinPage)om_CurrentBinPageRegion->init_addr; |
---|
[e70e45] | 116 | om_CurrentBinPageRegion->init_pages--; |
---|
[ff04713] | 117 | if (om_CurrentBinPageRegion->init_pages > 0) |
---|
[e70e45] | 118 | om_CurrentBinPageRegion->init_addr += SIZEOF_SYSTEM_PAGE; |
---|
| 119 | else |
---|
| 120 | om_CurrentBinPageRegion->init_addr = NULL; |
---|
| 121 | goto Found; |
---|
| 122 | } |
---|
| 123 | if (om_CurrentBinPageRegion->next != NULL) |
---|
| 124 | { |
---|
| 125 | om_CurrentBinPageRegion = om_CurrentBinPageRegion->next; |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | omBinPageRegion new_region = omAllocNewBinPagesRegion(1); |
---|
| 130 | new_region->prev = om_CurrentBinPageRegion; |
---|
| 131 | om_CurrentBinPageRegion->next = new_region; |
---|
| 132 | om_CurrentBinPageRegion = new_region; |
---|
| 133 | } |
---|
| 134 | } |
---|
[13fe1b] | 135 | |
---|
[e70e45] | 136 | Found: |
---|
| 137 | bin_page->region = om_CurrentBinPageRegion; |
---|
| 138 | om_CurrentBinPageRegion->used_pages++; |
---|
[13fe1b] | 139 | |
---|
[e70e45] | 140 | om_Info.UsedPages++; |
---|
| 141 | om_Info.AvailPages--; |
---|
| 142 | if (om_Info.UsedPages > om_Info.MaxPages) |
---|
| 143 | om_Info.MaxPages = om_Info.UsedPages; |
---|
[13fe1b] | 144 | |
---|
[e70e45] | 145 | OM_ALLOC_BINPAGE_HOOK; |
---|
| 146 | return bin_page; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | omBinPage omAllocBinPages(int how_many) |
---|
| 150 | { |
---|
| 151 | omBinPage bin_page; |
---|
| 152 | omBinPageRegion region; |
---|
| 153 | |
---|
| 154 | if (om_CurrentBinPageRegion == NULL) |
---|
| 155 | om_CurrentBinPageRegion = omAllocNewBinPagesRegion(how_many); |
---|
| 156 | |
---|
| 157 | region = om_CurrentBinPageRegion; |
---|
| 158 | while (1) |
---|
| 159 | { |
---|
| 160 | if (region->init_pages >= how_many) |
---|
| 161 | { |
---|
[13fe1b] | 162 | bin_page = (omBinPage)region->init_addr; |
---|
[e70e45] | 163 | region->init_pages -= how_many; |
---|
[13fe1b] | 164 | if (region->init_pages) |
---|
[e70e45] | 165 | region->init_addr += how_many*SIZEOF_SYSTEM_PAGE; |
---|
| 166 | else |
---|
| 167 | region->init_addr = NULL; |
---|
| 168 | goto Found; |
---|
| 169 | } |
---|
| 170 | if ((bin_page = omTakeOutConsecutivePages(region, how_many)) != NULL) |
---|
| 171 | { |
---|
| 172 | goto Found; |
---|
| 173 | } |
---|
| 174 | if (region->next != NULL) |
---|
| 175 | { |
---|
| 176 | region = region->next; |
---|
| 177 | } |
---|
| 178 | else |
---|
| 179 | { |
---|
| 180 | omBinPageRegion new_region = omAllocNewBinPagesRegion(how_many); |
---|
| 181 | region->next = new_region; |
---|
| 182 | new_region->prev = region; |
---|
| 183 | region = new_region; |
---|
| 184 | } |
---|
| 185 | } |
---|
[ff04713] | 186 | /*while (1) */ |
---|
[13fe1b] | 187 | |
---|
[e70e45] | 188 | Found: |
---|
| 189 | bin_page->region = region; |
---|
| 190 | region->used_pages += how_many; |
---|
| 191 | |
---|
| 192 | if (region != om_CurrentBinPageRegion && OM_IS_EMPTY_REGION(region)) |
---|
| 193 | { |
---|
| 194 | omTakeOutRegion(region); |
---|
| 195 | omInsertRegionBefore(region, om_CurrentBinPageRegion); |
---|
| 196 | } |
---|
| 197 | om_Info.UsedPages += how_many; |
---|
| 198 | om_Info.AvailPages -= how_many; |
---|
| 199 | if (om_Info.UsedPages > om_Info.MaxPages) |
---|
| 200 | om_Info.MaxPages = om_Info.UsedPages; |
---|
| 201 | |
---|
| 202 | OM_ALLOC_BINPAGE_HOOK; |
---|
| 203 | return bin_page; |
---|
| 204 | } |
---|
[13fe1b] | 205 | |
---|
[e70e45] | 206 | void omFreeBinPages(omBinPage bin_page, int how_many) |
---|
| 207 | { |
---|
| 208 | omBinPageRegion region = bin_page->region; |
---|
| 209 | |
---|
| 210 | region->used_pages -= how_many; |
---|
| 211 | if (region->used_pages == 0) |
---|
| 212 | { |
---|
[13fe1b] | 213 | if (region == om_CurrentBinPageRegion) |
---|
[e70e45] | 214 | { |
---|
[13fe1b] | 215 | if (region->next != NULL) |
---|
[e70e45] | 216 | om_CurrentBinPageRegion = region->next; |
---|
| 217 | else |
---|
| 218 | om_CurrentBinPageRegion = region->prev; |
---|
| 219 | } |
---|
| 220 | omTakeOutRegion(region); |
---|
| 221 | omFreeBinPagesRegion(region); |
---|
| 222 | } |
---|
| 223 | else |
---|
| 224 | { |
---|
| 225 | if (region != om_CurrentBinPageRegion && OM_IS_EMPTY_REGION(region)) |
---|
| 226 | { |
---|
| 227 | omTakeOutRegion(region); |
---|
| 228 | omInsertRegionAfter(region, om_CurrentBinPageRegion); |
---|
| 229 | } |
---|
| 230 | if (how_many > 1) |
---|
| 231 | { |
---|
| 232 | int i = how_many; |
---|
[13fe1b] | 233 | char* page = (char *)bin_page; |
---|
| 234 | |
---|
[e70e45] | 235 | while (i > 1) |
---|
| 236 | { |
---|
| 237 | NEXT_PAGE(page) = page + SIZEOF_SYSTEM_PAGE; |
---|
| 238 | page = NEXT_PAGE(page); |
---|
| 239 | i--; |
---|
| 240 | } |
---|
| 241 | NEXT_PAGE(page) = region->current; |
---|
| 242 | } |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | NEXT_PAGE(bin_page) = region->current; |
---|
| 246 | } |
---|
| 247 | region->current = (void*) bin_page; |
---|
| 248 | } |
---|
| 249 | om_Info.AvailPages += how_many; |
---|
| 250 | om_Info.UsedPages -= how_many; |
---|
| 251 | OM_FREE_BINPAGE_HOOK; |
---|
| 252 | } |
---|
[13fe1b] | 253 | |
---|
[e70e45] | 254 | static void* omTakeOutConsecutivePages(omBinPageRegion region, int pages) |
---|
| 255 | { |
---|
| 256 | void* current; |
---|
[13fe1b] | 257 | char* iter; |
---|
[e70e45] | 258 | void* prev = NULL; |
---|
| 259 | void* bin_page; |
---|
| 260 | int found; |
---|
| 261 | current = region->current; |
---|
| 262 | while (current != NULL) |
---|
| 263 | { |
---|
| 264 | found = 1; |
---|
| 265 | iter = current; |
---|
[13fe1b] | 266 | while (NEXT_PAGE(iter) == (iter + SIZEOF_SYSTEM_PAGE)) |
---|
[e70e45] | 267 | { |
---|
| 268 | iter = NEXT_PAGE(iter); |
---|
| 269 | /* handle pathological case that iter + SIZEOF_SYSTEM_PAGE == 0 */ |
---|
| 270 | if (iter == NULL) return NULL; |
---|
| 271 | found++; |
---|
| 272 | if (found == pages) |
---|
| 273 | { |
---|
| 274 | bin_page = current; |
---|
| 275 | if (current == region->current) |
---|
| 276 | { |
---|
| 277 | region->current = NEXT_PAGE(iter); |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | omAssume(prev != NULL); |
---|
| 282 | NEXT_PAGE(prev) = NEXT_PAGE(iter); |
---|
| 283 | } |
---|
| 284 | return bin_page; |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | prev = iter; |
---|
| 288 | current = NEXT_PAGE(iter); |
---|
| 289 | } |
---|
| 290 | return NULL; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /* Alloc a new region and insert into regions ring, set current to new region */ |
---|
| 294 | static omBinPageRegion omAllocNewBinPagesRegion(int min_pages) |
---|
| 295 | { |
---|
| 296 | omBinPageRegion region = omAllocFromSystem(sizeof(omBinPageRegion_t)); |
---|
[967895] | 297 | om_Info.InternalUsedBytesMalloc+=sizeof(omBinPageRegion_t); |
---|
[e70e45] | 298 | void* addr; |
---|
| 299 | int pages = (min_pages>om_Opts.PagesPerRegion ? min_pages : om_Opts.PagesPerRegion); |
---|
[35a2fb4] | 300 | size_t size = ((size_t)pages)*SIZEOF_SYSTEM_PAGE; |
---|
[13fe1b] | 301 | |
---|
[e70e45] | 302 | addr = _omVallocFromSystem(size, 1); |
---|
[13fe1b] | 303 | if (addr == NULL) |
---|
[e70e45] | 304 | { |
---|
| 305 | pages = min_pages; |
---|
| 306 | size = min_pages*SIZEOF_SYSTEM_PAGE; |
---|
| 307 | addr = omVallocFromSystem(size); |
---|
| 308 | } |
---|
[13fe1b] | 309 | |
---|
[e70e45] | 310 | omRegisterBinPages(addr, pages); |
---|
| 311 | region->addr = addr; |
---|
| 312 | region->pages = pages; |
---|
| 313 | region->used_pages = 0; |
---|
| 314 | region->init_addr = addr; |
---|
| 315 | region->init_pages = pages; |
---|
| 316 | region->current = NULL; |
---|
| 317 | region->next = NULL; |
---|
| 318 | region->prev = NULL; |
---|
| 319 | |
---|
| 320 | om_Info.AvailPages += pages; |
---|
| 321 | |
---|
| 322 | om_Info.CurrentRegionsAlloc++; |
---|
| 323 | if (om_Info.CurrentRegionsAlloc > om_Info.MaxRegionsAlloc) |
---|
| 324 | om_Info.MaxRegionsAlloc = om_Info.CurrentRegionsAlloc; |
---|
| 325 | |
---|
| 326 | return region; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | /* Free region */ |
---|
| 330 | static void omFreeBinPagesRegion(omBinPageRegion region) |
---|
| 331 | { |
---|
| 332 | omAssume(region != NULL && region->used_pages == 0); |
---|
| 333 | |
---|
| 334 | om_Info.AvailPages -= region->pages; |
---|
| 335 | om_Info.CurrentRegionsAlloc--; |
---|
| 336 | |
---|
| 337 | omUnregisterBinPages(region->addr, region->pages); |
---|
| 338 | omVfreeToSystem(region->addr, region->pages*SIZEOF_SYSTEM_PAGE); |
---|
| 339 | omFreeSizeToSystem(region, sizeof(omBinPageRegion_t)); |
---|
[967895] | 340 | om_Info.InternalUsedBytesMalloc-=sizeof(omBinPageRegion_t); |
---|
[e70e45] | 341 | } |
---|
| 342 | |
---|
| 343 | /******************************************************************* |
---|
[13fe1b] | 344 | * |
---|
[e70e45] | 345 | * BinPage registration |
---|
[13fe1b] | 346 | * |
---|
[e70e45] | 347 | *******************************************************************/ |
---|
| 348 | |
---|
| 349 | static void omBinPageIndexFault(unsigned long low_index, unsigned long high_index) |
---|
| 350 | { |
---|
| 351 | unsigned long index_diff = high_index - low_index; |
---|
| 352 | omAssume(low_index <= high_index && |
---|
| 353 | (high_index > om_MaxBinPageIndex || low_index < om_MinBinPageIndex)); |
---|
[13fe1b] | 354 | |
---|
[e70e45] | 355 | if (om_BinPageIndicies == NULL) |
---|
| 356 | { |
---|
[c4ea0bb] | 357 | unsigned long i; |
---|
[e70e45] | 358 | om_BinPageIndicies = (unsigned long*) omAllocFromSystem((index_diff + 1)*SIZEOF_LONG); |
---|
[77c252] | 359 | om_Info.InternalUsedBytesMalloc+=(index_diff + 1)*SIZEOF_LONG; |
---|
[e70e45] | 360 | om_MaxBinPageIndex = high_index; |
---|
| 361 | om_MinBinPageIndex = low_index; |
---|
| 362 | for (i=0; i<=index_diff; i++) om_BinPageIndicies[i] = 0; |
---|
| 363 | } |
---|
| 364 | else |
---|
| 365 | { |
---|
| 366 | unsigned long old_length = om_MaxBinPageIndex - om_MinBinPageIndex + 1; |
---|
[13fe1b] | 367 | unsigned long new_length = (low_index < om_MinBinPageIndex ? |
---|
| 368 | om_MaxBinPageIndex - low_index : |
---|
[e70e45] | 369 | high_index - om_MinBinPageIndex) + 1; |
---|
| 370 | om_BinPageIndicies = (unsigned long*) omReallocSizeFromSystem(om_BinPageIndicies, old_length*SIZEOF_LONG, |
---|
| 371 | new_length*SIZEOF_LONG); |
---|
[77c252] | 372 | om_Info.InternalUsedBytesMalloc+= (new_length-old_length)*SIZEOF_LONG; |
---|
[e70e45] | 373 | if (low_index < om_MinBinPageIndex) |
---|
| 374 | { |
---|
[c4ea0bb] | 375 | long i; |
---|
[e70e45] | 376 | unsigned long offset = new_length - old_length; |
---|
| 377 | for (i=old_length - 1; i >= 0; i--) om_BinPageIndicies[i+offset] = om_BinPageIndicies[i]; |
---|
[c4ea0bb] | 378 | for (i=offset-1; i>=0; i--) om_BinPageIndicies[i] = 0; |
---|
[e70e45] | 379 | om_MinBinPageIndex = low_index; |
---|
| 380 | } |
---|
| 381 | else |
---|
| 382 | { |
---|
[c4ea0bb] | 383 | unsigned long i; |
---|
[e70e45] | 384 | for (i=old_length; i<new_length; i++) om_BinPageIndicies[i] = 0; |
---|
| 385 | om_MaxBinPageIndex = high_index; |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | static void omRegisterBinPages(void* low_addr, int pages) |
---|
| 391 | { |
---|
| 392 | unsigned long low_index = omGetPageIndexOfAddr(low_addr); |
---|
[13fe1b] | 393 | char* high_addr = (char *)low_addr + (pages-1)*SIZEOF_SYSTEM_PAGE; |
---|
[e70e45] | 394 | unsigned long high_index = omGetPageIndexOfAddr(high_addr); |
---|
| 395 | unsigned long shift; |
---|
[13fe1b] | 396 | |
---|
[e70e45] | 397 | if (low_index < om_MinBinPageIndex || high_index > om_MaxBinPageIndex) |
---|
| 398 | omBinPageIndexFault(low_index, high_index); |
---|
| 399 | |
---|
| 400 | shift = omGetPageShiftOfAddr(low_addr); |
---|
| 401 | if (low_index < high_index) |
---|
| 402 | { |
---|
| 403 | if (shift == 0) |
---|
| 404 | { |
---|
| 405 | om_BinPageIndicies[low_index-om_MinBinPageIndex] = ULONG_MAX; |
---|
| 406 | } |
---|
| 407 | else |
---|
| 408 | { |
---|
[384936] | 409 | om_BinPageIndicies[low_index-om_MinBinPageIndex] |= ~ ((((unsigned long) 1) << shift) - 1); |
---|
[e70e45] | 410 | } |
---|
| 411 | for (shift = low_index+1; shift < high_index; shift++) |
---|
| 412 | { |
---|
| 413 | om_BinPageIndicies[shift-om_MinBinPageIndex] = ULONG_MAX; |
---|
| 414 | } |
---|
| 415 | shift = omGetPageShiftOfAddr(high_addr); |
---|
| 416 | if (shift == BIT_SIZEOF_LONG - 1) |
---|
| 417 | { |
---|
| 418 | om_BinPageIndicies[high_index - om_MinBinPageIndex] = ULONG_MAX; |
---|
| 419 | } |
---|
| 420 | else |
---|
| 421 | { |
---|
[384936] | 422 | om_BinPageIndicies[high_index-om_MinBinPageIndex] |= ((((unsigned long) 1) << (shift + 1)) - 1); |
---|
[e70e45] | 423 | } |
---|
| 424 | } |
---|
| 425 | else |
---|
| 426 | { |
---|
| 427 | high_index = omGetPageShiftOfAddr(high_addr); |
---|
| 428 | while (high_index > shift) |
---|
| 429 | { |
---|
[384936] | 430 | om_BinPageIndicies[low_index-om_MinBinPageIndex] |= (((unsigned long) 1) << high_index); |
---|
[e70e45] | 431 | high_index--; |
---|
| 432 | } |
---|
[384936] | 433 | om_BinPageIndicies[low_index-om_MinBinPageIndex] |= (((unsigned long) 1) << shift); |
---|
[e70e45] | 434 | } |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | static void omUnregisterBinPages(void* low_addr, int pages) |
---|
| 438 | { |
---|
| 439 | unsigned long low_index = omGetPageIndexOfAddr(low_addr); |
---|
[13fe1b] | 440 | char* high_addr = (char *)low_addr + (pages-1)*SIZEOF_SYSTEM_PAGE; |
---|
[e70e45] | 441 | unsigned long high_index = omGetPageIndexOfAddr(high_addr); |
---|
| 442 | unsigned long shift; |
---|
[13fe1b] | 443 | |
---|
[e70e45] | 444 | shift = omGetPageShiftOfAddr(low_addr); |
---|
| 445 | if (low_index < high_index) |
---|
| 446 | { |
---|
| 447 | if (shift == 0) |
---|
| 448 | { |
---|
| 449 | om_BinPageIndicies[low_index-om_MinBinPageIndex] = 0; |
---|
| 450 | } |
---|
| 451 | else |
---|
| 452 | { |
---|
[384936] | 453 | om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ((((unsigned long) 1) << shift) - 1); |
---|
[e70e45] | 454 | } |
---|
| 455 | for (shift = low_index+1; shift < high_index; shift++) |
---|
| 456 | { |
---|
| 457 | om_BinPageIndicies[shift-om_MinBinPageIndex] = 0; |
---|
| 458 | } |
---|
| 459 | shift = omGetPageShiftOfAddr(high_addr); |
---|
| 460 | if (shift == BIT_SIZEOF_LONG - 1) |
---|
| 461 | { |
---|
| 462 | om_BinPageIndicies[high_index - om_MinBinPageIndex] = 0; |
---|
| 463 | } |
---|
| 464 | else |
---|
| 465 | { |
---|
[384936] | 466 | om_BinPageIndicies[high_index-om_MinBinPageIndex] &= ~ ((((unsigned long) 1) << (shift + 1)) - 1); |
---|
[e70e45] | 467 | } |
---|
| 468 | } |
---|
| 469 | else |
---|
| 470 | { |
---|
| 471 | high_index = omGetPageShiftOfAddr(high_addr); |
---|
| 472 | while (high_index > shift) |
---|
| 473 | { |
---|
[384936] | 474 | om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ~(((unsigned long) 1) << high_index); |
---|
[e70e45] | 475 | high_index--; |
---|
| 476 | } |
---|
[384936] | 477 | om_BinPageIndicies[low_index-om_MinBinPageIndex] &= ~(((unsigned long) 1) << shift); |
---|
[e70e45] | 478 | } |
---|
| 479 | } |
---|
[13fe1b] | 480 | |
---|
[e70e45] | 481 | /*********************************************************************** |
---|
| 482 | * |
---|
| 483 | * checking routines |
---|
| 484 | * |
---|
| 485 | *******************************************************************/ |
---|
| 486 | #ifndef OM_NDEBUG |
---|
[be127e] | 487 | #include "omDebug.h" |
---|
[e70e45] | 488 | |
---|
| 489 | int omIsKnownMemoryRegion(omBinPageRegion region) |
---|
| 490 | { |
---|
| 491 | omBinPageRegion iter = om_CurrentBinPageRegion; |
---|
[13fe1b] | 492 | |
---|
[e70e45] | 493 | if (region == NULL || iter == NULL) return 0; |
---|
| 494 | iter = omGListLast(om_CurrentBinPageRegion, prev); |
---|
| 495 | do |
---|
| 496 | { |
---|
| 497 | if (region == iter) return 1; |
---|
| 498 | iter = iter->next; |
---|
| 499 | } |
---|
| 500 | while (iter != NULL); |
---|
| 501 | return 0; |
---|
| 502 | } |
---|
[13fe1b] | 503 | |
---|
[e70e45] | 504 | |
---|
| 505 | omError_t omCheckBinPageRegion(omBinPageRegion region, int level, omError_t report, OM_FLR_DECL) |
---|
| 506 | { |
---|
| 507 | if (level <= 0) return omError_NoError; |
---|
[13fe1b] | 508 | |
---|
[e70e45] | 509 | omCheckReturn(omCheckPtr(region, report, OM_FLR_VAL)); |
---|
| 510 | omCheckReturnCorrupted(! omIsKnownMemoryRegion(region)); |
---|
| 511 | omCheckReturnCorrupted(! omIsAddrPageAligned(region->addr) || ! omIsAddrPageAligned(region->current)); |
---|
| 512 | omCheckReturnCorrupted(region->used_pages < 0); |
---|
| 513 | omCheckReturnCorrupted(region->init_pages < 0 || region->init_pages > region->pages); |
---|
[13fe1b] | 514 | |
---|
[e70e45] | 515 | if (region->init_pages) |
---|
| 516 | { |
---|
| 517 | omCheckReturnCorrupted(! omIsAddrPageAligned(region->init_addr)); |
---|
[13fe1b] | 518 | omCheckReturnCorrupted(! (region->init_addr >= region->addr |
---|
[e70e45] | 519 | && region->init_addr <= region->addr + (region->pages -1)*SIZEOF_SYSTEM_PAGE)); |
---|
[13fe1b] | 520 | omCheckReturnCorrupted(region->init_addr != |
---|
[e70e45] | 521 | region->addr + (region->pages - region->init_pages)*SIZEOF_SYSTEM_PAGE); |
---|
| 522 | } |
---|
[13fe1b] | 523 | |
---|
[e70e45] | 524 | omCheckReturn(omCheckList(region->current, level, report, OM_FLR_VAL)); |
---|
| 525 | omCheckReturnCorrupted(region->current == NULL && region->used_pages + region->init_pages != region->pages); |
---|
[13fe1b] | 526 | omCheckReturnCorrupted(level > 1 && |
---|
[e70e45] | 527 | omListLength(region->current)+region->used_pages+region->init_pages != region->pages); |
---|
| 528 | return omError_NoError; |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | omError_t omCheckBinPageRegions(int level, omError_t report, OM_FLR_DECL) |
---|
| 532 | { |
---|
| 533 | omBinPageRegion iter = om_CurrentBinPageRegion; |
---|
| 534 | |
---|
| 535 | if (level <= 0) return omError_NoError; |
---|
| 536 | if (iter == NULL) return omError_NoError; |
---|
| 537 | |
---|
| 538 | omCheckReturnError(om_CurrentBinPageRegion->next != NULL && OM_IS_EMPTY_REGION(om_CurrentBinPageRegion->next), |
---|
| 539 | omError_InternalBug); |
---|
| 540 | omCheckReturnError(om_CurrentBinPageRegion->prev != NULL && ! OM_IS_EMPTY_REGION(om_CurrentBinPageRegion->prev), |
---|
| 541 | omError_InternalBug); |
---|
| 542 | |
---|
| 543 | |
---|
| 544 | if (level > 1) |
---|
| 545 | { |
---|
| 546 | omBinPageRegion prev_last = (omBinPageRegion) omGListLast(om_CurrentBinPageRegion, prev); |
---|
| 547 | omBinPageRegion next_last = (omBinPageRegion) omGListLast(om_CurrentBinPageRegion, next); |
---|
[13fe1b] | 548 | |
---|
[e70e45] | 549 | omCheckReturn(omCheckGList(iter, next, level, report, OM_FLR_VAL)); |
---|
| 550 | omCheckReturn(omCheckGList(iter, prev, level, report, OM_FLR_VAL)); |
---|
| 551 | |
---|
| 552 | omCheckReturnCorrupted(omGListLength(prev_last, next) |
---|
[13fe1b] | 553 | != |
---|
[e70e45] | 554 | omGListLength(next_last, prev)); |
---|
[13fe1b] | 555 | |
---|
[e70e45] | 556 | omCheckReturn(omCheckBinPageRegion(om_CurrentBinPageRegion, level - 1, report, OM_FLR_VAL)); |
---|
| 557 | |
---|
| 558 | iter = om_CurrentBinPageRegion->next; |
---|
| 559 | while (iter) |
---|
| 560 | { |
---|
| 561 | omCheckReturnError(OM_IS_EMPTY_REGION(iter), omError_InternalBug); |
---|
| 562 | |
---|
| 563 | omCheckReturn(omCheckBinPageRegion(iter, level - 1, report, OM_FLR_VAL)); |
---|
| 564 | iter = iter->next; |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | iter = om_CurrentBinPageRegion->prev; |
---|
| 568 | while (iter) |
---|
| 569 | { |
---|
| 570 | omCheckReturnError( !OM_IS_EMPTY_REGION(iter), omError_InternalBug); |
---|
| 571 | omCheckReturn(omCheckBinPageRegion(iter, level - 1, report, OM_FLR_VAL)); |
---|
| 572 | iter = iter->prev; |
---|
| 573 | } |
---|
| 574 | } |
---|
| 575 | return omError_NoError; |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | omBinPageRegion omFindRegionOfAddr(void* addr) |
---|
| 579 | { |
---|
| 580 | omBinPageRegion region = om_CurrentBinPageRegion; |
---|
[13fe1b] | 581 | |
---|
[e70e45] | 582 | if (region == NULL) return 0; |
---|
| 583 | region = omGListLast(region, prev); |
---|
| 584 | do |
---|
| 585 | { |
---|
[13fe1b] | 586 | if ((char *)addr >= region->addr |
---|
| 587 | && (char *)addr < region->addr + (region->pages)*SIZEOF_SYSTEM_PAGE) |
---|
[e70e45] | 588 | return region; |
---|
| 589 | region = region->next; |
---|
| 590 | } |
---|
| 591 | while (region != NULL); |
---|
| 592 | return NULL; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | int omIsAddrOnFreeBinPage(void* addr) |
---|
| 596 | { |
---|
[13fe1b] | 597 | char *c_addr=(char *)addr; |
---|
[e70e45] | 598 | omBinPageRegion region = om_CurrentBinPageRegion; |
---|
[13fe1b] | 599 | |
---|
[e70e45] | 600 | if (region == NULL) return 0; |
---|
| 601 | do |
---|
| 602 | { |
---|
[13fe1b] | 603 | if (c_addr > region->addr && c_addr < region->addr + (region->pages)*SIZEOF_SYSTEM_PAGE) |
---|
[e70e45] | 604 | { |
---|
| 605 | if (omIsOnList(region->current, omGetPageOfAddr(addr))) return 1; |
---|
| 606 | return 0; |
---|
| 607 | } |
---|
| 608 | region = region->next; |
---|
| 609 | } |
---|
| 610 | while (region != NULL); |
---|
| 611 | return 0; |
---|
| 612 | } |
---|
[13fe1b] | 613 | |
---|
[e70e45] | 614 | #endif /* ! OM_NDEBUG */ |
---|
[db143c] | 615 | #endif /* HAVE_OMALLOC*/ |
---|