Changeset 3576
- Timestamp:
- Sep 2, 2007, 10:23:54 PM (18 years ago)
- Location:
- trunk/kStuff
- Files:
-
- 7 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/kStuff/include/k/kHlpSem.h
r3573 r3576 39 39 #endif 40 40 41 KHLP_DECL(int) kHlpSemInit(void);42 KHLP_DECL(void) kHlpSemTerm(void);43 KHLP_DECL(int) kHlpSemRequest(void);44 KHLP_DECL(void) kHlpSemRelease(void);45 41 46 42 #ifdef __cplusplus -
trunk/kStuff/kHlp/Bare/kHlpBareHeap.c
r3573 r3576 25 25 */ 26 26 27 #define K LDRHEAP_STRICT27 #define KHLPHEAP_STRICT 28 28 29 29 /******************************************************************************* … … 31 31 *******************************************************************************/ 32 32 #include <k/kHlpAlloc.h> 33 #include <k/kHlpString.h> 33 34 34 35 #if K_OS == K_OS_OS2 … … 49 50 * A heap block. 50 51 */ 51 typedef struct K LDRHEAPBLOCK52 typedef struct KHLPHEAPBLOCK 52 53 { 53 54 /** Next block in the global list. */ 54 struct K LDRHEAPBLOCK *pNext;55 struct KHLPHEAPBLOCK *pNext; 55 56 /** Previous block in the global list. */ 56 struct K LDRHEAPBLOCK *pPrev;57 struct KHLPHEAPBLOCK *pPrev; 57 58 /** The size of this block including this header. */ 58 59 KSIZE cb; 59 60 /** The flags. */ 60 61 KSIZE fFlags; 61 } K LDRHEAPBLOCK, *PKLDRHEAPBLOCK;62 } KHLPHEAPBLOCK, *PKHLPHEAPBLOCK; 62 63 63 64 /** Indicates whether the block is free (set) or allocated (clear). */ 64 #define K LDRHEAPBLOCK_FLAG_FREE ((KSIZE)1)65 #define KHLPHEAPBLOCK_FLAG_FREE ((KSIZE)1) 65 66 /** Valid flag mask. */ 66 #define K LDRHEAPBLOCK_FLAG_MASK ((KSIZE)1)67 #define KHLPHEAPBLOCK_FLAG_MASK ((KSIZE)1) 67 68 68 69 /** Checks if the block is freed. */ 69 #define K LDRHEAPBLOCK_IS_FREE(pB) ( (pB)->fFlags & KLDRHEAPBLOCK_FLAG_FREE )70 #define KHLPHEAPBLOCK_IS_FREE(pB) ( (pB)->fFlags & KHLPHEAPBLOCK_FLAG_FREE ) 70 71 /** Check if the block is allocated. */ 71 #define K LDRHEAPBLOCK_IS_ALLOCATED(pB) !KLDRHEAPBLOCK_IS_FREE(pB)72 #define KHLPHEAPBLOCK_IS_ALLOCATED(pB) !KHLPHEAPBLOCK_IS_FREE(pB) 72 73 /** Checks if the two blocks are adjacent. 73 74 * Assumes pB1 < pB2. */ 74 #define K LDRHEAPBLOCK_IS_ADJACENT(pB1, pB2) \75 #define KHLPHEAPBLOCK_IS_ADJACENT(pB1, pB2) \ 75 76 ( ((KUPTR)(pB1) + (pB1)->cb) == (KUPTR)(pB2) ) 76 77 77 78 /** The block alignment. */ 78 #define K LDRHEAPBLOCK_ALIGNMENT sizeof(KLDRHEAPBLOCK)79 80 /** @def K LDRHEAP_ASSERT79 #define KHLPHEAPBLOCK_ALIGNMENT sizeof(KHLPHEAPBLOCK) 80 81 /** @def KHLPHEAP_ASSERT 81 82 * Heap assertion. */ 82 /** @def K LDRHEAP_ASSERT_BLOCK83 /** @def KHLPHEAP_ASSERT_BLOCK 83 84 * Assert that a heap block is valid. */ 84 /** @def K LDRHEAP_ASSERT_FREE85 /** @def KHLPHEAP_ASSERT_FREE 85 86 * Assert that a heap free block is valid. */ 86 #ifdef K LDRHEAP_STRICT87 # define K LDRHEAP_ASSERT(expr) kHlpAssert(expr)88 89 # define K LDRHEAP_ASSERT_BLOCK(pHeap, pBlock) \87 #ifdef KHLPHEAP_STRICT 88 # define KHLPHEAP_ASSERT(expr) kHlpAssert(expr) 89 90 # define KHLPHEAP_ASSERT_BLOCK(pHeap, pBlock) \ 90 91 do { \ 91 K LDRHEAP_ASSERT(!((pBlock)->fFlags & ~KLDRHEAPBLOCK_FLAG_MASK)); \92 K LDRHEAP_ASSERT(!((pBlock)->cb & (KLDRHEAPBLOCK_ALIGNMENT - 1))); \93 K LDRHEAP_ASSERT((KUPTR)(pBlock)->pPrev < (KUPTR)(pBlock)); \94 K LDRHEAP_ASSERT((KUPTR)(pBlock)->pNext > (KUPTR)(pBlock) || !(pBlock)->pNext); \92 KHLPHEAP_ASSERT(!((pBlock)->fFlags & ~KHLPHEAPBLOCK_FLAG_MASK)); \ 93 KHLPHEAP_ASSERT(!((pBlock)->cb & (KHLPHEAPBLOCK_ALIGNMENT - 1))); \ 94 KHLPHEAP_ASSERT((KUPTR)(pBlock)->pPrev < (KUPTR)(pBlock)); \ 95 KHLPHEAP_ASSERT((KUPTR)(pBlock)->pNext > (KUPTR)(pBlock) || !(pBlock)->pNext); \ 95 96 } while (0) 96 97 97 # define K LDRHEAP_ASSERT_FREE(pHeap, pFree) \98 # define KHLPHEAP_ASSERT_FREE(pHeap, pFree) \ 98 99 do { \ 99 K LDRHEAP_ASSERT_BLOCK(pHeap, &(pFree)->Core); \100 K LDRHEAP_ASSERT((KUPTR)(pFree)->pPrev < (KUPTR)(pFree)); \101 K LDRHEAP_ASSERT((KUPTR)(pFree)->pNext > (KUPTR)(pFree) || !(pFree)->pNext); \100 KHLPHEAP_ASSERT_BLOCK(pHeap, &(pFree)->Core); \ 101 KHLPHEAP_ASSERT((KUPTR)(pFree)->pPrev < (KUPTR)(pFree)); \ 102 KHLPHEAP_ASSERT((KUPTR)(pFree)->pNext > (KUPTR)(pFree) || !(pFree)->pNext); \ 102 103 } while (0) 103 104 104 105 #else 105 # define K LDRHEAP_ASSERT(expr) do { } while (0)106 # define K LDRHEAP_ASSERT_BLOCK(pH, pB) do { } while (0)107 # define K LDRHEAP_ASSERT_FREE(pH, pF) do { } while (0)106 # define KHLPHEAP_ASSERT(expr) do { } while (0) 107 # define KHLPHEAP_ASSERT_BLOCK(pH, pB) do { } while (0) 108 # define KHLPHEAP_ASSERT_FREE(pH, pF) do { } while (0) 108 109 #endif 109 110 … … 112 113 * A free heap block. 113 114 */ 114 typedef struct K LDRHEAPFREE115 typedef struct KHLPHEAPFREE 115 116 { 116 117 /** The core bit which we have in common with used blocks. */ 117 K LDRHEAPBLOCK Core;118 KHLPHEAPBLOCK Core; 118 119 /** The next free block. */ 119 struct K LDRHEAPFREE *pNext;120 struct KHLPHEAPFREE *pNext; 120 121 /** The previous free block. */ 121 struct K LDRHEAPFREE *pPrev;122 } K LDRHEAPFREE, *PKLDRHEAPFREE;122 struct KHLPHEAPFREE *pPrev; 123 } KHLPHEAPFREE, *PKHLPHEAPFREE; 123 124 124 125 … … 126 127 * A heap segment. 127 128 */ 128 typedef struct K LDRHEAPSEG129 typedef struct KHLPHEAPSEG 129 130 { 130 131 /** The base address of the segment. */ 131 void *pvBase;132 void *pvBase; 132 133 /** The length of the segment (in bytes). */ 133 KSIZE cb;134 } K LDRHEAPSEG, *PKLDRHEAPSEG;134 KSIZE cb; 135 } KHLPHEAPSEG, *PKHLPHEAPSEG; 135 136 136 137 /** 137 138 * Bundle of heap segments. 138 139 */ 139 typedef struct K LDRHEAPSEGS140 typedef struct KHLPHEAPSEGS 140 141 { 141 142 /** Pointer to the next segment bundle. */ 142 struct K LDRHEAPSEGS *pNext;143 struct KHLPHEAPSEGS *pNext; 143 144 /** The number of segments used. */ 144 145 KU32 cSegs; 145 146 /** Array of chunks. */ 146 K LDRHEAPSEG aSegs[64];147 } K LDRHEAPSEGS, *PKLDRHEAPSEGS;147 KHLPHEAPSEG aSegs[64]; 148 } KHLPHEAPSEGS, *PKHLPHEAPSEGS; 148 149 149 150 … … 151 152 * Heap anchor block. 152 153 */ 153 typedef struct K LDRHEAPANCHOR154 typedef struct KHLPHEAPANCHOR 154 155 { 155 156 /** Head of the block list. */ 156 PK LDRHEAPBLOCKpHead;157 PKHLPHEAPBLOCK pHead; 157 158 /** Tail of the block list. */ 158 PK LDRHEAPBLOCKpTail;159 PKHLPHEAPBLOCK pTail; 159 160 /** Head of the free list. */ 160 PK LDRHEAPFREEpFreeHead;161 PKHLPHEAPFREE pFreeHead; 161 162 /** Head segment bundle. 162 163 * The order of this list is important, but a bit peculiar. 163 164 * Logically, SegsHead::pNext is the tail pointer. */ 164 K LDRHEAPSEGSSegsHead;165 } K LDRHEAPANCHOR, *PKLDRHEAPANCHOR;165 KHLPHEAPSEGS SegsHead; 166 } KHLPHEAPANCHOR, *PKHLPHEAPANCHOR; 166 167 167 168 … … 171 172 *******************************************************************************/ 172 173 /** The heap anchor block. */ 173 static K LDRHEAPANCHORg_Heap;174 static KHLPHEAPANCHOR g_Heap; 174 175 175 176 … … 177 178 * Internal Functions * 178 179 *******************************************************************************/ 179 static int kLdrHeapInit(PKLDRHEAPANCHOR pHeap); 180 static void kLdrHeapDelete(PKLDRHEAPANCHOR pHeap); 181 static void * kLdrHeapAlloc(PKLDRHEAPANCHOR pHeap, KSIZE cb); 182 static void kLdrHeapFree(PKLDRHEAPANCHOR pHeap, void *pv); 183 static void kLdrHeapDonate(PKLDRHEAPANCHOR pHeap, void *pv, KSIZE cb); 184 static int kLdrHeapSegAlloc(PKLDRHEAPSEG pSeg, KSIZE cb); 185 static void kLdrHeapSegFree(PKLDRHEAPSEG pSeg); 180 static int khlpHeapInit(PKHLPHEAPANCHOR pHeap); 181 static void khlpHeapDelete(PKHLPHEAPANCHOR pHeap); 182 static void * khlpHeapAlloc(PKHLPHEAPANCHOR pHeap, KSIZE cb); 183 static void khlpHeapFree(PKHLPHEAPANCHOR pHeap, void *pv); 184 static KSIZE khlpHeapBlockSize(PKHLPHEAPANCHOR pHeap, void *pv); 185 static void khlpHeapDonate(PKHLPHEAPANCHOR pHeap, void *pv, KSIZE cb); 186 static int khlpHeapSegAlloc(PKHLPHEAPSEG pSeg, KSIZE cb); 187 static void khlpHeapSegFree(PKHLPHEAPSEG pSeg); 186 188 187 189 … … 193 195 KHLP_DECL(int) kHlpHeapInit(void) 194 196 { 195 return k LdrHeapInit(&g_Heap);197 return khlpHeapInit(&g_Heap); 196 198 } 197 199 … … 202 204 KHLP_DECL(void) kHlpHeapTerm(void) 203 205 { 204 kLdrHeapDelete(&g_Heap); 205 } 206 207 208 /** 209 * Allocates memory from the kLdr heap. 210 * 211 * @returns Pointer to the allocated heap block. NULL if we can't satify the request. 212 * @param cb The requested heap block size. 213 */ 206 khlpHeapDelete(&g_Heap); 207 } 208 209 214 210 KHLP_DECL(void *) kHlpAlloc(KSIZE cb) 215 211 { 216 return kLdrHeapAlloc(&g_Heap, cb); 217 } 218 219 220 /** 221 * Allocates zero initialized memory from the kLdr heap. 222 * 223 * @returns Pointer to the allocated heap block. NULL if we can't satify the request. 224 * @param cb The requested heap block size. 225 */ 212 return khlpHeapAlloc(&g_Heap, cb); 213 } 214 215 226 216 KHLP_DECL(void *) kHlpAllocZ(KSIZE cb) 227 217 { 228 void *pv = k LdrHeapAlloc(&g_Heap, cb);218 void *pv = khlpHeapAlloc(&g_Heap, cb); 229 219 if (pv) 230 220 kHlpMemSet(pv, 0, cb); … … 233 223 234 224 235 /** 236 * Frees memory allocated off the kLdr heap. 237 * 238 * @param pv Pointer to the heap block returned by kHlpAlloc(). 239 */ 225 KHLP_DECL(void *) kHlpDup(const void *pv, KSIZE cb) 226 { 227 void *pvNew = khlpHeapAlloc(&g_Heap, cb); 228 if (pvNew) 229 kHlpMemCopy(pvNew, pv, cb); 230 return pvNew; 231 } 232 233 234 KHLP_DECL(char *) kHlpStrDup(const char *psz) 235 { 236 return (char *)kHlpDup(psz, kHlpStrLen(psz) + 1); 237 } 238 239 240 KHLP_DECL(void *) kHlpRealloc(void *pv, KSIZE cb) 241 { 242 void *pvNew; 243 if (!cb) 244 { 245 kHlpFree(pv); 246 pvNew = NULL; 247 } 248 else if (!pv) 249 pvNew = khlpHeapAlloc(&g_Heap, cb); 250 else 251 { 252 KSIZE cbToCopy = khlpHeapBlockSize(&g_Heap, pv); 253 pvNew = khlpHeapAlloc(&g_Heap, cb); 254 if (pvNew) 255 { 256 kHlpMemCopy(pvNew, pv, cb); 257 kHlpFree(pv); 258 } 259 } 260 return pvNew; 261 } 262 263 240 264 KHLP_DECL(void) kHlpFree(void *pv) 241 265 { 242 k LdrHeapFree(&g_Heap, pv);266 khlpHeapFree(&g_Heap, pv); 243 267 } 244 268 … … 252 276 KHLP_DECL(void) kHlpHeapDonate(void *pv, KSIZE cb) 253 277 { 254 k LdrHeapDonate(&g_Heap, pv, cb);278 khlpHeapDonate(&g_Heap, pv, cb); 255 279 } 256 280 … … 263 287 * @param pHeap The heap anchor to be initialized. 264 288 */ 265 static int k LdrHeapInit(PKLDRHEAPANCHOR pHeap)289 static int khlpHeapInit(PKHLPHEAPANCHOR pHeap) 266 290 { 267 291 pHeap->pHead = NULL; … … 280 304 * @param pHeap The heap to be deleted. 281 305 */ 282 static void k LdrHeapDelete(PKLDRHEAPANCHOR pHeap)306 static void khlpHeapDelete(PKHLPHEAPANCHOR pHeap) 283 307 { 284 308 /* … … 292 316 /* find the tail. */ 293 317 KU32 iSeg; 294 PK LDRHEAPSEGS pSegs = pHeap->SegsHead.pNext;318 PKHLPHEAPSEGS pSegs = pHeap->SegsHead.pNext; 295 319 if (!pSegs) 296 320 pSegs = &pHeap->SegsHead; … … 304 328 iSeg = pSegs->cSegs; 305 329 while (iSeg-- > 0) 306 k LdrHeapSegFree(&pSegs->aSegs[iSeg]);330 khlpHeapSegFree(&pSegs->aSegs[iSeg]); 307 331 pSegs->cSegs = 0; 308 332 } … … 320 344 * Internal heap block allocator. 321 345 */ 322 static void * kldrHeapAllocSub(PK LDRHEAPANCHOR pHeap, KSIZE cb)346 static void * kldrHeapAllocSub(PKHLPHEAPANCHOR pHeap, KSIZE cb) 323 347 { 324 348 /* 325 349 * Find a fitting free block. 326 350 */ 327 const KSIZE cbReq = K_ALIGN_Z(cb + sizeof(K LDRHEAPBLOCK), KLDRHEAPBLOCK_ALIGNMENT);328 PK LDRHEAPFREE pCur = pHeap->pFreeHead;351 const KSIZE cbReq = K_ALIGN_Z(cb + sizeof(KHLPHEAPBLOCK), KHLPHEAPBLOCK_ALIGNMENT); 352 PKHLPHEAPFREE pCur = pHeap->pFreeHead; 329 353 while (pCur) 330 354 { … … 334 358 { 335 359 /* check and see if there is a better match close by. */ 336 PK LDRHEAPFREE pCur2 = pCur->pNext;360 PKHLPHEAPFREE pCur2 = pCur->pNext; 337 361 unsigned i = 16; 338 362 while (i-- > 0 && pCur2) … … 350 374 351 375 /* next */ 352 K LDRHEAP_ASSERT_FREE(pHeap, pCur2);376 KHLPHEAP_ASSERT_FREE(pHeap, pCur2); 353 377 pCur2 = pCur2->pNext; 354 378 } … … 358 382 359 383 /* next */ 360 K LDRHEAP_ASSERT_FREE(pHeap, pCur);384 KHLPHEAP_ASSERT_FREE(pHeap, pCur); 361 385 pCur = pCur->pNext; 362 386 } 363 387 if (!pCur) 364 388 return NULL; 365 K LDRHEAP_ASSERT_FREE(pHeap, pCur);389 KHLPHEAP_ASSERT_FREE(pHeap, pCur); 366 390 367 391 /* 368 392 * Do we need to split out a block? 369 393 */ 370 if (pCur->Core.cb - cbReq >= K LDRHEAPBLOCK_ALIGNMENT * 2)371 { 372 PK LDRHEAPBLOCK pNew;394 if (pCur->Core.cb - cbReq >= KHLPHEAPBLOCK_ALIGNMENT * 2) 395 { 396 PKHLPHEAPBLOCK pNew; 373 397 374 398 pCur->Core.cb -= cbReq; 375 399 376 pNew = (PK LDRHEAPBLOCK)((KUPTR)pCur + pCur->Core.cb);400 pNew = (PKHLPHEAPBLOCK)((KUPTR)pCur + pCur->Core.cb); 377 401 pNew->fFlags = 0; 378 402 pNew->cb = cbReq; … … 385 409 pCur->Core.pNext = pNew; 386 410 387 K LDRHEAP_ASSERT_FREE(pHeap, pCur);388 K LDRHEAP_ASSERT_BLOCK(pHeap, pNew);411 KHLPHEAP_ASSERT_FREE(pHeap, pCur); 412 KHLPHEAP_ASSERT_BLOCK(pHeap, pNew); 389 413 return pNew + 1; 390 414 } … … 399 423 else 400 424 pHeap->pFreeHead = pCur->pNext; 401 pCur->Core.fFlags &= ~K LDRHEAPBLOCK_FLAG_FREE;402 403 K LDRHEAP_ASSERT_BLOCK(pHeap, &pCur->Core);425 pCur->Core.fFlags &= ~KHLPHEAPBLOCK_FLAG_FREE; 426 427 KHLPHEAP_ASSERT_BLOCK(pHeap, &pCur->Core); 404 428 return &pCur->Core + 1; 405 429 } … … 413 437 * @param cb The requested heap block size. 414 438 */ 415 static void * k LdrHeapAlloc(PKLDRHEAPANCHOR pHeap, KSIZE cb)439 static void * khlpHeapAlloc(PKHLPHEAPANCHOR pHeap, KSIZE cb) 416 440 { 417 441 void *pv; 418 442 419 443 /* adjust the requested block size. */ 420 cb = K_ALIGN_Z(cb, K LDRHEAPBLOCK_ALIGNMENT);444 cb = K_ALIGN_Z(cb, KHLPHEAPBLOCK_ALIGNMENT); 421 445 if (!cb) 422 cb = K LDRHEAPBLOCK_ALIGNMENT;446 cb = KHLPHEAPBLOCK_ALIGNMENT; 423 447 424 448 /* try allocate the block. */ … … 429 453 * Failed, add another segment and try again. 430 454 */ 431 K LDRHEAPSEG Seg;432 if (k LdrHeapSegAlloc(&Seg, cb + sizeof(KLDRHEAPSEGS) + sizeof(KLDRHEAPBLOCK) * 16))455 KHLPHEAPSEG Seg; 456 if (khlpHeapSegAlloc(&Seg, cb + sizeof(KHLPHEAPSEGS) + sizeof(KHLPHEAPBLOCK) * 16)) 433 457 return NULL; 434 458 435 459 /* donate before insterting the segment, this makes sure we got heap to expand the segment list. */ 436 k LdrHeapDonate(pHeap, Seg.pvBase, Seg.cb);460 khlpHeapDonate(pHeap, Seg.pvBase, Seg.cb); 437 461 438 462 /* insert the segment. */ … … 444 468 else 445 469 { 446 PK LDRHEAPSEGS pSegs = (PKLDRHEAPSEGS)kldrHeapAllocSub(pHeap, sizeof(*pSegs));447 K LDRHEAP_ASSERT(pSegs);470 PKHLPHEAPSEGS pSegs = (PKHLPHEAPSEGS)kldrHeapAllocSub(pHeap, sizeof(*pSegs)); 471 KHLPHEAP_ASSERT(pSegs); 448 472 pSegs->pNext = pHeap->SegsHead.pNext; 449 473 pHeap->SegsHead.pNext = pSegs; … … 454 478 /* retry (should succeed) */ 455 479 pv = kldrHeapAllocSub(pHeap, cb); 456 K LDRHEAP_ASSERT(pv);480 KHLPHEAP_ASSERT(pv); 457 481 } 458 482 … … 465 489 * 466 490 * @param pHeap The heap. 467 * @param pv The pointer returned by k LdrHeapAlloc().468 */ 469 static void k LdrHeapFree(PKLDRHEAPANCHOR pHeap, void *pv)470 { 471 PK LDRHEAPFREE pFree, pLeft, pRight;491 * @param pv The pointer returned by khlpHeapAlloc(). 492 */ 493 static void khlpHeapFree(PKHLPHEAPANCHOR pHeap, void *pv) 494 { 495 PKHLPHEAPFREE pFree, pLeft, pRight; 472 496 473 497 /* ignore NULL pointers. */ … … 475 499 return; 476 500 477 pFree = (PK LDRHEAPFREE)((PKLDRHEAPBLOCK)pv - 1);478 K LDRHEAP_ASSERT_BLOCK(pHeap, &pFree->Core);479 K LDRHEAP_ASSERT(KLDRHEAPBLOCK_IS_ALLOCATED(&pFree->Core));501 pFree = (PKHLPHEAPFREE)((PKHLPHEAPBLOCK)pv - 1); 502 KHLPHEAP_ASSERT_BLOCK(pHeap, &pFree->Core); 503 KHLPHEAP_ASSERT(KHLPHEAPBLOCK_IS_ALLOCATED(&pFree->Core)); 480 504 481 505 /* 482 506 * Merge or link with left node? 483 507 */ 484 pLeft = (PK LDRHEAPFREE)pFree->Core.pPrev;508 pLeft = (PKHLPHEAPFREE)pFree->Core.pPrev; 485 509 if ( pLeft 486 && K LDRHEAPBLOCK_IS_FREE(&pLeft->Core)487 && K LDRHEAPBLOCK_IS_ADJACENT(&pLeft->Core, &pFree->Core)510 && KHLPHEAPBLOCK_IS_FREE(&pLeft->Core) 511 && KHLPHEAPBLOCK_IS_ADJACENT(&pLeft->Core, &pFree->Core) 488 512 ) 489 513 { … … 502 526 { 503 527 /* link left */ 504 while (pLeft && !K LDRHEAPBLOCK_IS_FREE(&pLeft->Core))505 pLeft = (PK LDRHEAPFREE)pLeft->Core.pPrev;528 while (pLeft && !KHLPHEAPBLOCK_IS_FREE(&pLeft->Core)) 529 pLeft = (PKHLPHEAPFREE)pLeft->Core.pPrev; 506 530 if (pLeft) 507 531 { … … 520 544 pHeap->pFreeHead = pFree; 521 545 } 522 pFree->Core.fFlags |= K LDRHEAPBLOCK_FLAG_FREE;523 } 524 K LDRHEAP_ASSERT_FREE(pHeap, pFree);546 pFree->Core.fFlags |= KHLPHEAPBLOCK_FLAG_FREE; 547 } 548 KHLPHEAP_ASSERT_FREE(pHeap, pFree); 525 549 526 550 /* 527 551 * Merge right? 528 552 */ 529 pRight = (PK LDRHEAPFREE)pFree->Core.pNext;553 pRight = (PKHLPHEAPFREE)pFree->Core.pNext; 530 554 if ( pRight 531 && K LDRHEAPBLOCK_IS_FREE(&pRight->Core)532 && K LDRHEAPBLOCK_IS_ADJACENT(&pFree->Core, pRight)555 && KHLPHEAPBLOCK_IS_FREE(&pRight->Core) 556 && KHLPHEAPBLOCK_IS_ADJACENT(&pFree->Core, pRight) 533 557 ) 534 558 { … … 553 577 554 578 /** 579 * Calcs the size of a heap block. 580 * 581 * @returns The block size (in bytes). 582 * @param pHeap The heap. 583 * @param pv Pointer to an in-use heap block. 584 */ 585 static KSIZE khlpHeapBlockSize(PKHLPHEAPANCHOR pHeap, void *pv) 586 { 587 PKHLPHEAPBLOCK pBlock = (PKHLPHEAPBLOCK)pv - 1; 588 KHLPHEAP_ASSERT_BLOCK(pHeap, pBlock); 589 KHLPHEAP_ASSERT(KHLPHEAPBLOCK_IS_ALLOCATED(pBlock)); 590 return (KU8 *)pBlock->pNext - (KU8 *)pv; 591 } 592 593 594 /** 555 595 * Donates memory to the heap. 556 596 * … … 561 601 * @param cb Size of the donated memory. 562 602 */ 563 static void k LdrHeapDonate(PKLDRHEAPANCHOR pHeap, void *pv, KSIZE cb)564 { 565 PK LDRHEAPBLOCK pBlock;603 static void khlpHeapDonate(PKHLPHEAPANCHOR pHeap, void *pv, KSIZE cb) 604 { 605 PKHLPHEAPBLOCK pBlock; 566 606 567 607 /* 568 608 * Don't bother with small donations. 569 609 */ 570 if (cb < K LDRHEAPBLOCK_ALIGNMENT * 4)610 if (cb < KHLPHEAPBLOCK_ALIGNMENT * 4) 571 611 return; 572 612 … … 574 614 * Align the donation on a heap block boundrary. 575 615 */ 576 if ((KUPTR)pv & (K LDRHEAPBLOCK_ALIGNMENT - 1))616 if ((KUPTR)pv & (KHLPHEAPBLOCK_ALIGNMENT - 1)) 577 617 { 578 618 cb -= (KUPTR)pv & 31; 579 pv = K_ALIGN_P(pv, K LDRHEAPBLOCK_ALIGNMENT);580 } 581 cb &= ~(KSIZE)(K LDRHEAPBLOCK_ALIGNMENT - 1);619 pv = K_ALIGN_P(pv, KHLPHEAPBLOCK_ALIGNMENT); 620 } 621 cb &= ~(KSIZE)(KHLPHEAPBLOCK_ALIGNMENT - 1); 582 622 583 623 /* 584 624 * Create an allocated block, link it and free it. 585 625 */ 586 pBlock = (PK LDRHEAPBLOCK)pv;626 pBlock = (PKHLPHEAPBLOCK)pv; 587 627 pBlock->pNext = NULL; 588 628 pBlock->pPrev = NULL; … … 617 657 { 618 658 /* in list (unlikely) */ 619 PK LDRHEAPBLOCK pPrev = pHeap->pHead;620 PK LDRHEAPBLOCK pCur = pPrev->pNext;659 PKHLPHEAPBLOCK pPrev = pHeap->pHead; 660 PKHLPHEAPBLOCK pCur = pPrev->pNext; 621 661 for (;;) 622 662 { 623 K LDRHEAP_ASSERT_BLOCK(pHeap, pCur);663 KHLPHEAP_ASSERT_BLOCK(pHeap, pCur); 624 664 if ((KUPTR)pCur > (KUPTR)pBlock) 625 665 break; … … 633 673 pCur->pPrev = pBlock; 634 674 } 635 K LDRHEAP_ASSERT_BLOCK(pHeap, pBlock);675 KHLPHEAP_ASSERT_BLOCK(pHeap, pBlock); 636 676 637 677 /* free it */ 638 k LdrHeapFree(pHeap, pBlock + 1);678 khlpHeapFree(pHeap, pBlock + 1); 639 679 } 640 680 … … 648 688 * @param cbMin The minimum segment size. 649 689 */ 650 static int k LdrHeapSegAlloc(PKLDRHEAPSEG pSeg, KSIZE cbMin)690 static int khlpHeapSegAlloc(PKHLPHEAPSEG pSeg, KSIZE cbMin) 651 691 { 652 692 #if K_OS == K_OS_OS2 … … 687 727 * @param pSeg The segment to be freed. 688 728 */ 689 static void k LdrHeapSegFree(PKLDRHEAPSEG pSeg)729 static void khlpHeapSegFree(PKHLPHEAPSEG pSeg) 690 730 { 691 731 #if K_OS == K_OS_OS2 692 732 APIRET rc = DosFreeMem(pSeg->pvBase); 693 K LDRHEAP_ASSERT(!rc); (void)rc;733 KHLPHEAP_ASSERT(!rc); (void)rc; 694 734 695 735 #elif K_OS == K_OS_WINDOWS 696 736 BOOL fRc = VirtualFree(pSeg->pvBase, 0 /*pSeg->cb*/, MEM_RELEASE); 697 K LDRHEAP_ASSERT(fRc); (void)fRc;737 KHLPHEAP_ASSERT(fRc); (void)fRc; 698 738 699 739 #else -
trunk/kStuff/kHlp/Makefile.kmk
r3575 r3576 76 76 Bare/kHlpBareEnv.c \ 77 77 Bare/kHlpBareProcess.c \ 78 Bare/kHlpBareSem.c \79 78 Bare/kHlpBareThread.c \ 80 79 -
trunk/kStuff/kLdr/Makefile.kmk
r3573 r3576 119 119 kLdrDyldMod.c \ 120 120 kLdrDyldOS.c \ 121 kLdrSem.c \ 121 122 kLdrErr.c \ 122 123 kLdrMisc.c \ -
trunk/kStuff/kLdr/kLdr.c
r3573 r3576 103 103 if (!rc) 104 104 { 105 rc = k HlpSemInit();105 rc = kLdrDyldSemInit(); 106 106 if (!rc) 107 107 { … … 112 112 return 0; 113 113 } 114 k HlpSemTerm();114 kLdrDyldSemTerm(); 115 115 } 116 116 kHlpHeapTerm(); … … 134 134 * Do the termination. 135 135 */ 136 k HlpSemTerm();136 kLdrDyldSemTerm(); 137 137 kHlpHeapTerm(); 138 138 -
trunk/kStuff/kLdr/kLdrDyld.c
r3573 r3576 280 280 * Make sure we own the loader semaphore (necessary for init). 281 281 */ 282 rc = k HlpSemRequest();282 rc = kLdrDyldSemRequest(); 283 283 if (rc) 284 284 kldrDyldFailure(rc, "Sem req. failure, rc=%d", rc); … … 339 339 340 340 /* get the semaphore and do the job. */ 341 rc = k HlpSemRequest();341 rc = kLdrDyldSemRequest(); 342 342 if (!rc) 343 343 { … … 348 348 g_cActiveLoadCalls--; 349 349 kldrDyldDoModuleTerminationAndGarabageCollection(); 350 k HlpSemRelease();350 kLdrDyldSemRelease(); 351 351 *phMod = pMod ? pMod->hMod : NIL_HKLDRMOD; 352 352 } … … 369 369 370 370 /* get sem & do work */ 371 rc = k HlpSemRequest();371 rc = kLdrDyldSemRequest(); 372 372 if (!rc) 373 373 { … … 377 377 g_cActiveUnloadCalls--; 378 378 kldrDyldDoModuleTerminationAndGarabageCollection(); 379 k HlpSemRelease();379 kLdrDyldSemRelease(); 380 380 } 381 381 return rc; … … 408 408 409 409 /* get sem & do work */ 410 rc = k HlpSemRequest();410 rc = kLdrDyldSemRequest(); 411 411 if (!rc) 412 412 { 413 413 PKLDRDYLDMOD pMod = NULL; 414 414 rc = kldrDyldDoFindByName(pszDll, pszPrefix, pszSuffix, enmSearch, fFlags, &pMod); 415 k HlpSemRelease();415 kLdrDyldSemRelease(); 416 416 *phMod = pMod ? pMod->hMod : NIL_HKLDRMOD; 417 417 } … … 445 445 446 446 /* get sem & do work */ 447 rc = k HlpSemRequest();447 rc = kLdrDyldSemRequest(); 448 448 if (!rc) 449 449 { 450 450 PKLDRDYLDMOD pMod = NULL; 451 451 rc = kldrDyldDoFindByAddress(Address, &pMod, piSegment, poffSegment); 452 k HlpSemRelease();452 kLdrDyldSemRelease(); 453 453 *phMod = pMod ? pMod->hMod : NIL_HKLDRMOD; 454 454 } … … 478 478 479 479 /* get sem & do work */ 480 rc = k HlpSemRequest();480 rc = kLdrDyldSemRequest(); 481 481 if (!rc) 482 482 { 483 483 rc = kldrDyldDoGetName(hMod, pszName, cchName); 484 k HlpSemRelease();484 kLdrDyldSemRelease(); 485 485 } 486 486 return rc; … … 509 509 510 510 /* get sem & do work */ 511 rc = k HlpSemRequest();511 rc = kLdrDyldSemRequest(); 512 512 if (!rc) 513 513 { 514 514 rc = kldrDyldDoGetFilename(hMod, pszFilename, cchFilename); 515 k HlpSemRelease();515 kLdrDyldSemRelease(); 516 516 } 517 517 return rc; … … 547 547 548 548 /* get sem & do work */ 549 rc = k HlpSemRequest();549 rc = kLdrDyldSemRequest(); 550 550 if (!rc) 551 551 { 552 552 rc = kldrDyldDoQuerySymbol(hMod, uSymbolOrdinal, pszSymbolName, pValue, pfKind); 553 k HlpSemRelease();553 kLdrDyldSemRelease(); 554 554 } 555 555 return rc; -
trunk/kStuff/kLdr/kLdrInternal.h
r3573 r3576 436 436 437 437 438 /** @name The Loader semaphore 439 * @{ */ 440 int kLdrDyldSemInit(void); 441 void kLdrDyldSemTerm(void); 442 int kLdrDyldSemRequest(void); 443 void kLdrDyldSemRelease(void); 444 /** @} */ 445 446 438 447 /** @name Module interpreter method tables 439 448 * @{ */ -
trunk/kStuff/kLdr/kLdrSem.c
r3573 r3576 64 64 * @returns 0 on success, non-zero OS status code on failure. 65 65 */ 66 int k HlpSemInit(void)66 int kLdrDyldSemInit(void) 67 67 { 68 68 #if K_OS == K_OS_OS2 … … 86 86 * Terminates the loader semaphore. 87 87 */ 88 void k HlpSemTerm(void)88 void kLdrDyldSemTerm(void) 89 89 { 90 90 #if K_OS == K_OS_OS2 … … 108 108 * @returns 0 on success, non-zero OS status code on failure. 109 109 */ 110 int k HlpSemRequest(void)110 int kLdrDyldSemRequest(void) 111 111 { 112 112 #if K_OS == K_OS_OS2 … … 140 140 * The caller is responsible for making sure it's the semaphore owner! 141 141 */ 142 void k HlpSemRelease(void)142 void kLdrDyldSemRelease(void) 143 143 { 144 144 #if K_OS == K_OS_OS2
Note:
See TracChangeset
for help on using the changeset viewer.