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