4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Huw Davies
10 * All local heap functions need the current DS as first parameter
11 * when called from the emulation library, so they take one more
12 * parameter than usual.
24 #include "stackframe.h"
36 WORD prev; /* Previous arena | arena type */
37 WORD next; /* Next arena */
38 /* Start of the memory block or free-list info */
39 WORD size; /* Size of the free block */
40 WORD free_prev; /* Previous free block */
41 WORD free_next; /* Next free block */
44 #define ARENA_HEADER_SIZE 4
45 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
47 /* Arena types (stored in 'prev' field of the arena) */
48 #define LOCAL_ARENA_FREE 0
49 #define LOCAL_ARENA_FIXED 1
51 /* Layout of a handle entry table
53 * WORD count of entries
54 * LOCALHANDLEENTRY[count] entries
55 * WORD near ptr to next table
59 WORD addr; /* Address of the MOVEABLE block */
60 BYTE flags; /* Flags for this block */
61 BYTE lock; /* Lock count */
66 WORD check; /* 00 Heap checking flag */
67 WORD freeze; /* 02 Heap frozen flag */
68 WORD items; /* 04 Count of items on the heap */
69 WORD first; /* 06 First item of the heap */
70 WORD pad1; /* 08 Always 0 */
71 WORD last; /* 0a Last item of the heap */
72 WORD pad2; /* 0c Always 0 */
73 BYTE ncompact; /* 0e Compactions counter */
74 BYTE dislevel; /* 0f Discard level */
75 DWORD distotal; /* 10 Total bytes discarded */
76 WORD htable; /* 14 Pointer to handle table */
77 WORD hfree; /* 16 Pointer to free handle table */
78 WORD hdelta; /* 18 Delta to expand the handle table */
79 WORD expand; /* 1a Pointer to expand function (unused) */
80 WORD pstat; /* 1c Pointer to status structure (unused) */
81 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
82 WORD lock; /* 22 Lock count for the heap */
83 WORD extra; /* 24 Extra bytes to allocate when expanding */
84 WORD minsize; /* 26 Minimum size of the heap */
85 WORD magic; /* 28 Magic number */
92 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
95 /* All local heap allocations are aligned on 4-byte boundaries */
96 #define LALIGN(word) (((word) + 3) & ~3)
98 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
99 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
100 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
101 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
103 /* determine whether the handle belongs to a fixed or a moveable block */
104 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
105 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
107 /***********************************************************************
110 * Return a pointer to the local heap, making sure it exists.
112 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
114 LOCALHEAPINFO *pInfo;
115 INSTANCEDATA *ptr = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( ds, 0 );
116 dprintf_local( stddeb, "Heap at %p, %04x\n", ptr, ptr->heap );
117 if (!ptr || !ptr->heap) return NULL;
118 if (IsBadReadPtr((SEGPTR)MAKELONG( ptr->heap, ds ), sizeof(LOCALHEAPINFO)))
120 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
121 if (pInfo->magic != LOCAL_HEAP_MAGIC) return NULL;
126 /***********************************************************************
127 * LOCAL_MakeBlockFree
129 * Make a block free, inserting it in the free-list.
130 * 'block' is the handle of the block arena; 'baseptr' points to
131 * the beginning of the data segment containing the heap.
133 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
135 LOCALARENA *pArena, *pNext;
138 /* Mark the block as free */
140 pArena = ARENA_PTR( baseptr, block );
141 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
142 pArena->size = pArena->next - block;
144 /* Find the next free block (last block is always free) */
149 pNext = ARENA_PTR( baseptr, next );
150 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
154 dprintf_local( stddeb, "Local_AddFreeBlock %04x, next %04x\n", block, next );
155 /* Insert the free block in the free-list */
157 pArena->free_prev = pNext->free_prev;
158 pArena->free_next = next;
159 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
160 pNext->free_prev = block;
164 /***********************************************************************
165 * LOCAL_RemoveFreeBlock
167 * Remove a block from the free-list.
168 * 'block' is the handle of the block arena; 'baseptr' points to
169 * the beginning of the data segment containing the heap.
171 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
173 /* Mark the block as fixed */
175 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
176 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
178 /* Remove it from the list */
180 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
181 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
185 /***********************************************************************
188 * Insert a new block in the heap.
189 * 'new' is the handle of the new block arena; 'baseptr' points to
190 * the beginning of the data segment containing the heap; 'prev' is
191 * the block before the new one.
193 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
195 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
196 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
198 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
199 pNew->next = pPrev->next;
200 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
201 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
206 /***********************************************************************
209 * Remove a block from the heap.
210 * 'block' is the handle of the block arena; 'baseptr' points to
211 * the beginning of the data segment containing the heap.
213 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
215 LOCALARENA *pArena, *pTmp;
217 /* Remove the block from the free-list */
219 dprintf_local( stddeb, "Local_RemoveBlock\n");
220 pArena = ARENA_PTR( baseptr, block );
221 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
222 LOCAL_RemoveFreeBlock( baseptr, block );
224 /* If the previous block is free, expand its size */
226 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
227 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
228 pTmp->size += pArena->next - block;
230 /* Remove the block from the linked list */
232 pTmp->next = pArena->next;
233 pTmp = ARENA_PTR( baseptr, pArena->next );
234 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
238 /***********************************************************************
241 static void LOCAL_PrintHeap( HANDLE16 ds )
243 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
244 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
247 if (!debugging_local) return;
250 printf( "Local Heap corrupted! ds=%04x\n", ds );
253 printf( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
254 ds, pInfo->first, pInfo->last, pInfo->items );
256 arena = pInfo->first;
259 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
260 printf( " %04x: prev=%04x next=%04x type=%d\n", arena,
261 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
262 if (arena == pInfo->first)
264 printf( " size=%d free_prev=%04x free_next=%04x\n",
265 pArena->size, pArena->free_prev, pArena->free_next );
267 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
269 printf( " size=%d free_prev=%04x free_next=%04x\n",
270 pArena->size, pArena->free_prev, pArena->free_next );
271 if (pArena->next == arena) break; /* last one */
272 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
274 printf( "*** arena->free_next->free_prev != arena\n" );
278 if (pArena->next == arena)
280 printf( "*** last block is not marked free\n" );
283 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
285 printf( "*** arena->next->prev != arena (%04x, %04x)\n",
286 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
289 arena = pArena->next;
294 /***********************************************************************
295 * LocalInit (KERNEL.4)
297 BOOL16 LocalInit( HANDLE16 selector, WORD start, WORD end )
300 WORD heapInfoArena, freeArena, lastArena;
301 LOCALHEAPINFO *pHeapInfo;
302 LOCALARENA *pArena, *pFirstArena, *pLastArena;
305 /* The initial layout of the heap is: */
306 /* - first arena (FIXED) */
307 /* - heap info structure (FIXED) */
308 /* - large free block (FREE) */
309 /* - last arena (FREE) */
311 dprintf_local(stddeb, "LocalInit: %04x %04x-%04x\n", selector, start, end);
312 if (!selector) selector = CURRENT_DS;
316 /* If debugging_heap is set, the global heap blocks are cleared */
317 /* before use, so we can test for double initialization. */
318 if (LOCAL_GetHeap(selector))
320 fprintf( stderr, "LocalInit: Heap %04x initialized twice.\n", selector);
321 if (debugging_local) LOCAL_PrintHeap(selector);
326 /* Check if the segment is the DGROUP of a module */
328 if ((pModule = MODULE_GetPtr( GetExePtr( selector ) )))
330 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule ) + pModule->dgroup - 1;
331 if (pModule->dgroup && (pSeg->selector == selector)) {
332 /* We can't just use the simple method of using the value
333 * of minsize + stacksize, since there are programs that
334 * resize the data segment before calling InitTask(). So,
335 * we must put it at the end of the segment */
336 start = GlobalSize16( GlobalHandle16( selector ) );
339 dprintf_local( stddeb," new start %04x, minstart: %04x\n", start, pSeg->minsize + pModule->stack_size);
343 ptr = PTR_SEG_OFF_TO_LIN( selector, 0 );
345 start = LALIGN( MAX( start, sizeof(INSTANCEDATA) ) );
346 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
347 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
348 + sizeof(LOCALHEAPINFO) );
349 lastArena = (end - sizeof(LOCALARENA)) & ~3;
351 /* Make sure there's enough space. */
353 if (freeArena + sizeof(LOCALARENA) >= lastArena) return FALSE;
355 /* Initialise the first arena */
357 pFirstArena = ARENA_PTR( ptr, start );
358 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
359 pFirstArena->next = heapInfoArena;
360 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
361 pFirstArena->free_prev = start; /* this one */
362 pFirstArena->free_next = freeArena;
364 /* Initialise the arena of the heap info structure */
366 pArena = ARENA_PTR( ptr, heapInfoArena );
367 pArena->prev = start | LOCAL_ARENA_FIXED;
368 pArena->next = freeArena;
370 /* Initialise the heap info structure */
372 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
373 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
374 pHeapInfo->items = 4;
375 pHeapInfo->first = start;
376 pHeapInfo->last = lastArena;
377 pHeapInfo->htable = 0;
378 pHeapInfo->hdelta = 0x20;
379 pHeapInfo->extra = 0x200;
380 pHeapInfo->minsize = lastArena - freeArena;
381 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
383 /* Initialise the large free block */
385 pArena = ARENA_PTR( ptr, freeArena );
386 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
387 pArena->next = lastArena;
388 pArena->size = lastArena - freeArena;
389 pArena->free_prev = start;
390 pArena->free_next = lastArena;
392 /* Initialise the last block */
394 pLastArena = ARENA_PTR( ptr, lastArena );
395 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
396 pLastArena->next = lastArena; /* this one */
397 pLastArena->size = LALIGN(sizeof(LOCALARENA));
398 pLastArena->free_prev = freeArena;
399 pLastArena->free_next = lastArena; /* this one */
401 /* Store the local heap address in the instance data */
403 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
404 LOCAL_PrintHeap( selector );
408 /***********************************************************************
411 static void LOCAL_GrowHeap( HANDLE16 ds )
413 HANDLE16 hseg = GlobalHandle16( ds );
414 LONG oldsize = GlobalSize16( hseg );
416 LOCALHEAPINFO *pHeapInfo;
417 WORD freeArena, lastArena;
418 LOCALARENA *pArena, *pLastArena;
421 /* if nothing can be gained, return */
422 if (oldsize > 0xfff0) return;
423 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
424 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
425 pHeapInfo = LOCAL_GetHeap( ds );
426 if (pHeapInfo == NULL) {
427 fprintf( stderr, "Local_GrowHeap: heap not found\n" );
430 end = GlobalSize16( hseg );
431 lastArena = (end - sizeof(LOCALARENA)) & ~3;
433 /* Update the HeapInfo */
435 freeArena = pHeapInfo->last;
436 pHeapInfo->last = lastArena;
437 pHeapInfo->minsize += end - oldsize;
439 /* grow the old last block */
440 pArena = ARENA_PTR( ptr, freeArena );
441 pArena->size = lastArena - freeArena;
442 pArena->next = lastArena;
443 pArena->free_next = lastArena;
445 /* Initialise the new last block */
447 pLastArena = ARENA_PTR( ptr, lastArena );
448 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
449 pLastArena->next = lastArena; /* this one */
450 pLastArena->size = LALIGN(sizeof(LOCALARENA));
451 pLastArena->free_prev = freeArena;
452 pLastArena->free_next = lastArena; /* this one */
454 /* If block before freeArena is also free then merge them */
455 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
457 LOCAL_RemoveBlock(ptr, freeArena);
461 dprintf_local( stddeb, "Heap expanded\n" );
462 LOCAL_PrintHeap( ds );
466 /***********************************************************************
469 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
471 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
472 LOCALHEAPINFO *pInfo;
473 LOCALARENA *pArena, *pPrev, *pNext;
475 dprintf_local( stddeb, "LocalFreeArena: %04x ds=%04x\n", arena, ds );
476 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
478 pArena = ARENA_PTR( ptr, arena );
479 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
481 /* shouldn't happen */
482 fprintf( stderr, "LocalFreeArena: Trying to free block %04x twice!\n",
484 LOCAL_PrintHeap( ds );
488 /* Check if we can merge with the previous block */
490 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
491 pNext = ARENA_PTR( ptr, pArena->next );
492 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
494 arena = pArena->prev & ~3;
496 LOCAL_RemoveBlock( ptr, pPrev->next );
499 else /* Make a new free block */
501 LOCAL_MakeBlockFree( ptr, arena );
504 /* Check if we can merge with the next block */
506 if ((pArena->next == pArena->free_next) &&
507 (pArena->next != pInfo->last))
509 LOCAL_RemoveBlock( ptr, pArena->next );
516 /***********************************************************************
519 * Shrink an arena by creating a free block at its end if possible.
520 * 'size' includes the arena header, and must be aligned.
522 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
524 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
525 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
527 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
529 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
531 LOCAL_AddBlock( ptr, arena, arena + size );
533 LOCAL_FreeArena( ds, arena + size );
538 /***********************************************************************
539 * LOCAL_GrowArenaDownward
541 * Grow an arena downward by using the previous arena (must be free).
543 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
545 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
546 LOCALHEAPINFO *pInfo;
547 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
548 WORD prevArena = pArena->prev & ~3;
549 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
553 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
554 offset = pPrevArena->size;
555 size = pArena->next - arena - ARENA_HEADER_SIZE;
556 LOCAL_RemoveFreeBlock( ptr, prevArena );
557 LOCAL_RemoveBlock( ptr, arena );
559 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
560 while (offset < size)
562 memcpy( p, p + offset, offset );
566 if (size) memcpy( p, p + offset, size );
567 LOCAL_ShrinkArena( ds, prevArena, newsize );
571 /***********************************************************************
574 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
576 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
577 LOCALHEAPINFO *pInfo;
582 if (!(pInfo = LOCAL_GetHeap( ds )))
584 fprintf( stderr, "LOCAL_GetFreeSpace: Local heap not found\n" );
588 arena = pInfo->first;
589 pArena = ARENA_PTR( ptr, arena );
590 while (arena != pArena->free_next)
592 arena = pArena->free_next;
593 pArena = ARENA_PTR( ptr, arena );
594 if (pArena->size >= freespace) freespace = pArena->size;
596 /* FIXME doesn't yet calculate space that would become free if everything
597 were discarded when countdiscard == 1 */
598 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
599 else freespace -= ARENA_HEADER_SIZE;
604 /***********************************************************************
607 static WORD LOCAL_Compact( HANDLE16 ds, WORD minfree, WORD flags )
609 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
610 LOCALHEAPINFO *pInfo;
611 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
612 WORD arena, movearena, finalarena, table;
613 WORD count, movesize, size;
615 LOCALHANDLEENTRY *pEntry;
617 if (!(pInfo = LOCAL_GetHeap( ds )))
619 fprintf( stderr, "Local_Compact: Local heap not found\n" );
623 dprintf_local(stddeb,
624 "LOCAL_Compact: ds = %04x, minfree = %04x, flags = %04x\n",
626 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
627 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
629 dprintf_local(stddeb, "Returning %04x.\n", freespace);
632 dprintf_local(stddeb, "Local_Compact: Compacting heap %04x.\n", ds);
633 table = pInfo->htable;
636 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
637 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
639 if((pEntry->lock == 0) && !(pEntry->flags &
640 (LMEM_DISCARDED >> 8)))
642 /* OK we can move this one if we want */
643 dprintf_local(stddeb,
644 "handle %04x (block %04x) can be moved.\n",
645 (WORD)((char *)pEntry - ptr), pEntry->addr);
646 movearena = ARENA_HEADER(pEntry->addr);
647 pMoveArena = ARENA_PTR(ptr, movearena);
648 movesize = pMoveArena->next - movearena;
649 arena = pInfo->first;
650 pArena = ARENA_PTR(ptr, arena);
653 /* Try to find the smallest arena that will do, */
654 /* which is below us in memory */
657 arena = pArena->free_next;
658 pArena = ARENA_PTR(ptr, arena);
659 if(arena >= movearena)
661 if(arena == pArena->free_next)
663 if((pArena->size >= movesize) && (pArena->size < size))
669 if (finalarena) /* Actually got somewhere to move */
671 dprintf_local(stddeb, "Moving it to %04x.\n", finalarena);
672 pFinalArena = ARENA_PTR(ptr, finalarena);
673 size = pFinalArena->size;
674 LOCAL_RemoveFreeBlock(ptr, finalarena);
675 LOCAL_ShrinkArena( ds, finalarena, movesize );
676 /* Copy the arena to it's new location */
677 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
678 (char *)pMoveArena + ARENA_HEADER_SIZE,
679 movesize - ARENA_HEADER_SIZE );
680 /* Free the old location */
681 LOCAL_FreeArena(ds, movearena);
682 /* Update handle table entry */
683 pEntry->addr = finalarena + ARENA_HEADER_SIZE;
685 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
688 /* Previous arena is free (but < movesize) */
689 /* so we can 'slide' movearena down into it */
690 finalarena = pMoveArena->prev & ~3;
691 LOCAL_GrowArenaDownward( ds, movearena, movesize );
692 /* Update handle table entry */
693 pEntry->addr = finalarena + ARENA_HEADER_SIZE;
697 table = *(WORD *)pEntry;
699 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
700 if(freespace >= minfree || (flags & LMEM_NODISCARD))
702 dprintf_local(stddeb, "Returning %04x.\n", freespace);
706 table = pInfo->htable;
709 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
710 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
712 if((pEntry->flags & (LMEM_DISCARDABLE >> 8)) &&
713 pEntry->lock == 0 && !(pEntry->flags & (LMEM_DISCARDED >> 8)))
715 dprintf_local(stddeb, "Discarding handle %04x (block %04x).\n",
716 (char *)pEntry - ptr, pEntry->addr);
717 LOCAL_FreeArena(ds, pEntry->addr);
719 pEntry->flags |= (LMEM_DISCARDED >> 8);
720 /* Call localnotify proc */
723 table = *(WORD *)pEntry;
725 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
729 /***********************************************************************
730 * LOCAL_FindFreeBlock
732 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
734 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
735 LOCALHEAPINFO *pInfo;
739 if (!(pInfo = LOCAL_GetHeap( ds )))
741 fprintf( stderr, "Local_FindFreeBlock: Local heap not found\n" );
746 arena = pInfo->first;
747 pArena = ARENA_PTR( ptr, arena );
749 arena = pArena->free_next;
750 pArena = ARENA_PTR( ptr, arena );
751 if (arena == pArena->free_next) break;
752 if (pArena->size >= size) return arena;
754 dprintf_local( stddeb, "Local_FindFreeBlock: not enough space\n" );
755 if (debugging_local) LOCAL_PrintHeap(ds);
759 /***********************************************************************
761 * The segment may get moved around in this function, so all callers
762 * should reset their pointer variables.
764 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
766 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
767 LOCALHEAPINFO *pInfo;
771 if (!(pInfo = LOCAL_GetHeap( ds )))
773 fprintf( stderr, "Local_GetBlock: Local heap not found\n");
778 size += ARENA_HEADER_SIZE;
779 size = LALIGN( MAX( size, sizeof(LOCALARENA) ) );
781 /* Find a suitable free block */
782 arena = LOCAL_FindFreeBlock( ds, size );
784 /* no space: try to make some */
785 LOCAL_Compact( ds, size, flags );
786 arena = LOCAL_FindFreeBlock( ds, size );
789 /* still no space: try to grow the segment */
790 LOCAL_GrowHeap( ds );
791 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
792 pInfo = LOCAL_GetHeap( ds );
793 arena = LOCAL_FindFreeBlock( ds, size );
796 fprintf( stderr, "Local_GetBlock: not enough space in heap %04x for %d bytes\n",
801 /* Make a block out of the free arena */
802 pArena = ARENA_PTR( ptr, arena );
803 dprintf_local(stddeb, "LOCAL_GetBlock size = %04x, arena %04x size %04x\n",
804 size, arena, pArena->size );
805 LOCAL_RemoveFreeBlock( ptr, arena );
806 LOCAL_ShrinkArena( ds, arena, size );
808 if (flags & LMEM_ZEROINIT) memset( (char *)pArena + ARENA_HEADER_SIZE, 0,
809 size - ARENA_HEADER_SIZE );
810 return arena + ARENA_HEADER_SIZE;
814 /***********************************************************************
817 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
819 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
820 LOCALHEAPINFO *pInfo;
821 LOCALHANDLEENTRY *pEntry;
825 dprintf_local( stddeb, "Local_NewHTable\n" );
826 if (!(pInfo = LOCAL_GetHeap( ds )))
828 fprintf( stderr, "Local heap not found\n");
833 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
834 + 2 * sizeof(WORD), LMEM_FIXED )))
836 if (!(ptr = PTR_SEG_OFF_TO_LIN( ds, 0 )))
837 fprintf(stderr, "LOCAL_NewHTable: ptr == NULL after GetBlock.\n");
838 if (!(pInfo = LOCAL_GetHeap( ds )))
839 fprintf(stderr,"LOCAL_NewHTable: pInfo == NULL after GetBlock.\n");
841 /* Fill the entry table */
843 *(WORD *)(ptr + handle) = pInfo->hdelta;
844 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
845 for (i = pInfo->hdelta; i > 0; i--) (pEntry++)->lock = 0xff;
846 *(WORD *)pEntry = pInfo->htable;
847 pInfo->htable = handle;
852 /***********************************************************************
853 * LOCAL_GetNewHandleEntry
855 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
857 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
858 LOCALHEAPINFO *pInfo;
859 LOCALHANDLEENTRY *pEntry = NULL;
862 if (!(pInfo = LOCAL_GetHeap( ds )))
864 fprintf( stderr, "LOCAL_GetNewHandleEntry: Local heap not found\n");
869 /* Find a free slot in existing tables */
871 table = pInfo->htable;
874 WORD count = *(WORD *)(ptr + table);
875 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
876 for (; count > 0; count--, pEntry++)
877 if (pEntry->lock == 0xff) break;
879 table = *(WORD *)pEntry;
882 if (!table) /* We need to create a new table */
884 if (!LOCAL_NewHTable( ds )) return 0;
885 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
886 pInfo = LOCAL_GetHeap( ds );
887 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
890 /* Now allocate this entry */
893 dprintf_local( stddeb, "LOCAL_GetNewHandleEntry(%04x): %04x\n",
894 ds, ((char *)pEntry - ptr) );
895 return (HLOCAL16)((char *)pEntry - ptr);
899 /***********************************************************************
900 * LOCAL_FreeHandleEntry
902 * Free a handle table entry.
904 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
906 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
907 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
908 LOCALHEAPINFO *pInfo;
910 WORD table, count, i;
912 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
914 /* Find the table where this handle comes from */
916 pTable = &pInfo->htable;
919 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
920 if ((handle >= *pTable + sizeof(WORD)) &&
921 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
922 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
926 fprintf(stderr, "LOCAL_FreeHandleEntry: invalid entry %04x\n", handle);
927 LOCAL_PrintHeap( ds );
931 /* Make the entry free */
933 pEntry->addr = 0; /* just in case */
936 /* Now check if all entries in this table are free */
939 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
940 count = *(WORD *)(ptr + table);
941 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
943 /* Remove the table from the linked list and free it */
945 dprintf_local( stddeb, "LOCAL_FreeHandleEntry(%04x): freeing table %04x\n",
947 *pTable = *(WORD *)pEntry;
948 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
952 /***********************************************************************
955 * Implementation of LocalFree().
957 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
959 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
961 dprintf_local( stddeb, "LocalFree: %04x ds=%04x\n", handle, ds );
963 if (!handle) { fprintf( stderr, "LOCAL_Free: handle is 0.\n" ); return 0; }
964 if (HANDLE_FIXED( handle ))
966 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
967 else return handle; /* couldn't free it */
971 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
972 if (!(pEntry->flags & (LMEM_DISCARDED >> 8)))
974 dprintf_local( stddeb, "LocalFree: real block at %04x\n",
976 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr) ))
977 return handle; /* couldn't free it */
979 LOCAL_FreeHandleEntry( ds, handle );
985 /***********************************************************************
988 * Implementation of LocalAlloc().
990 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
995 dprintf_local( stddeb, "LocalAlloc: %04x %d ds=%04x\n", flags, size, ds );
997 if (flags & LMEM_MOVEABLE)
999 LOCALHANDLEENTRY *plhe;
1002 if (!(hmem = LOCAL_GetBlock( ds, size, flags ))) return 0;
1003 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1005 fprintf( stderr, "LocalAlloc: couldn't get handle\n");
1006 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1009 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1010 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1012 plhe->flags = (BYTE)(flags >> 8);
1015 else handle = LOCAL_GetBlock( ds, size, flags );
1021 /***********************************************************************
1024 * Implementation of LocalReAlloc().
1026 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1028 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1029 LOCALHEAPINFO *pInfo;
1030 LOCALARENA *pArena, *pNext;
1031 LOCALHANDLEENTRY *pEntry;
1032 WORD arena, newhandle, blockhandle, oldsize;
1035 if (!handle) return LOCAL_Alloc( ds, size, flags );
1037 dprintf_local( stddeb, "LocalReAlloc: %04x %d %04x ds=%04x\n",
1038 handle, size, flags, ds );
1039 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1041 if (HANDLE_FIXED( handle )) blockhandle = handle;
1044 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1045 if(pEntry->flags & (LMEM_DISCARDED >> 8))
1047 dprintf_local(stddeb, "ReAllocating discarded block\n");
1048 if (!(pEntry->addr = LOCAL_GetBlock( ds, size, flags))) return 0;
1049 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1050 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1051 pEntry->flags = (BYTE) (flags >> 8);
1055 if (!(blockhandle = pEntry->addr))
1057 fprintf( stderr, "Local_ReAlloc(%04x,%04x): invalid handle\n",
1063 arena = ARENA_HEADER( blockhandle );
1064 dprintf_local( stddeb, "LocalReAlloc: arena is %04x\n", arena );
1065 pArena = ARENA_PTR( ptr, arena );
1067 if ((flags & LMEM_MODIFY) && (flags & LMEM_DISCARDABLE))
1069 if (HANDLE_FIXED(handle))
1071 fprintf(stderr,"LocalReAlloc: LMEM_MODIFY & LMEM_DISCARDABLE on a fixed handle.\n");
1074 dprintf_local( stddeb, "Making block discardable.\n" );
1075 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1076 pEntry->flags |= (LMEM_DISCARDABLE >> 8);
1080 if ((flags & LMEM_MODIFY) || (flags & LMEM_DISCARDABLE))
1082 fprintf(stderr,"LocalReAlloc: flags %04x. MODIFY & DISCARDABLE should both be set\n", flags);
1088 if (HANDLE_FIXED(handle))
1090 if (flags & LMEM_MOVEABLE)
1092 dprintf_local(stddeb, "Freeing fixed block.\n");
1093 return LOCAL_Free( ds, handle );
1099 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1100 if (pEntry->lock == 0)
1102 /* discards moveable blocks is this right? */
1103 dprintf_local(stddeb,"Discarding block\n");
1104 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
1106 pEntry->flags = (LMEM_DISCARDED >> 8);
1113 size = LALIGN( size );
1114 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1115 nextarena = LALIGN(blockhandle + size);
1117 /* Check for size reduction */
1119 if (nextarena <= pArena->next)
1121 if (nextarena < pArena->next - LALIGN(sizeof(LOCALARENA)))
1123 dprintf_local( stddeb, "size reduction, making new free block\n");
1124 /* It is worth making a new free block */
1125 LOCAL_AddBlock( ptr, arena, nextarena );
1127 LOCAL_FreeArena( ds, nextarena );
1129 dprintf_local( stddeb, "LocalReAlloc: returning %04x\n", handle );
1133 /* Check if the next block is free */
1135 pNext = ARENA_PTR( ptr, pArena->next );
1136 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1137 (nextarena <= pNext->next))
1139 LOCAL_RemoveBlock( ptr, pArena->next );
1140 if (nextarena < pArena->next - LALIGN(sizeof(LOCALARENA)))
1142 dprintf_local( stddeb, "size increase, making new free block\n");
1143 /* It is worth making a new free block */
1144 LOCAL_AddBlock( ptr, arena, nextarena );
1146 LOCAL_FreeArena( ds, nextarena );
1148 dprintf_local( stddeb, "LocalReAlloc: returning %04x\n", handle );
1152 /* Now we have to allocate a new block, but not if fixed block and no
1155 if (HANDLE_FIXED(handle) && !(flags & LMEM_MOVEABLE))
1157 dprintf_local(stddeb, "Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1158 return 0; /* FIXME: should we free it here? */
1161 newhandle = LOCAL_GetBlock( ds, size, flags );
1162 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1165 /* Remove the block from the heap and try again */
1166 LPSTR buffer = HeapAlloc( SystemHeap, 0, oldsize );
1167 if (!buffer) return 0;
1168 memcpy( buffer, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1169 LOCAL_FreeArena( ds, arena );
1170 if (!(newhandle = LOCAL_GetBlock( ds, size, flags )))
1172 if (!(newhandle = LOCAL_GetBlock( ds, oldsize, flags )))
1174 fprintf( stderr, "LocalRealloc: can't restore saved block\n" );
1175 HeapFree( SystemHeap, 0, buffer );
1180 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1181 memcpy( ptr + newhandle, buffer, oldsize );
1182 HeapFree( SystemHeap, 0, buffer );
1186 memcpy( ptr + newhandle, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1187 LOCAL_FreeArena( ds, arena );
1189 if (HANDLE_MOVEABLE( handle ))
1191 dprintf_local( stddeb, "LocalReAlloc: fixing handle\n");
1192 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1193 pEntry->addr = newhandle;
1197 if (size == oldsize) newhandle = 0; /* Realloc failed */
1198 dprintf_local( stddeb, "LocalReAlloc: returning %04x\n", newhandle );
1203 /***********************************************************************
1204 * LOCAL_InternalLock
1206 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1208 dprintf_local( stddeb, "LocalLock: %04x ", handle );
1209 if (HANDLE_MOVEABLE(handle))
1211 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1212 if (pEntry->lock < 0xfe) pEntry->lock++;
1213 handle = pEntry->addr;
1215 dprintf_local( stddeb, "returning %04x\n", handle );
1220 /***********************************************************************
1223 LPSTR LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1225 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1226 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1230 /***********************************************************************
1233 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1235 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1237 dprintf_local( stddeb, "LocalUnlock: %04x\n", handle );
1238 if (HANDLE_MOVEABLE(handle))
1240 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1241 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1242 /* For moveable block, return the new lock count */
1243 /* (see _Windows_Internals_ p. 197) */
1244 return --pEntry->lock;
1250 /***********************************************************************
1253 * Implementation of LocalSize().
1255 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1257 char *ptr = PTR_SEG_OFF_TO_LIN( CURRENT_DS, 0 );
1260 dprintf_local( stddeb, "LocalSize: %04x ds=%04x\n", handle, ds );
1262 if (HANDLE_MOVEABLE( handle )) handle = *(WORD *)(ptr + handle);
1263 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1264 return pArena->next - handle;
1268 /***********************************************************************
1271 * Implementation of LocalFlags().
1273 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1275 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1277 if (HANDLE_MOVEABLE(handle))
1279 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1280 dprintf_local( stddeb, "LOCAL_Flags(%04x,%04x): returning %04x\n",
1281 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1282 return pEntry->lock | (pEntry->flags << 8);
1286 dprintf_local( stddeb, "LOCAL_Flags(%04x,%04x): returning 0\n",
1293 /***********************************************************************
1296 * Implementation of LocalHeapSize().
1298 WORD LOCAL_HeapSize( HANDLE16 ds )
1300 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1301 if (!pInfo) return 0;
1302 return pInfo->last - pInfo->first;
1306 /***********************************************************************
1309 * Implementation of LocalCountFree().
1311 WORD LOCAL_CountFree( HANDLE16 ds )
1315 LOCALHEAPINFO *pInfo;
1316 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1318 if (!(pInfo = LOCAL_GetHeap( ds )))
1320 fprintf( stderr, "LOCAL_Handle(%04x): Local heap not found\n", ds );
1321 LOCAL_PrintHeap( ds );
1326 arena = pInfo->first;
1327 pArena = ARENA_PTR( ptr, arena );
1330 arena = pArena->free_next;
1331 pArena = ARENA_PTR( ptr, arena );
1332 if (arena == pArena->free_next) break;
1333 total += pArena->size;
1335 dprintf_local( stddeb, "LOCAL_CountFree(%04x): returning %d\n", ds, total);
1340 /***********************************************************************
1343 * Implementation of LocalHandle().
1345 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1347 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1348 LOCALHEAPINFO *pInfo;
1351 if (!(pInfo = LOCAL_GetHeap( ds )))
1353 fprintf( stderr, "LOCAL_Handle(%04x): Local heap not found\n", ds );
1354 LOCAL_PrintHeap( ds );
1358 /* Find the address in the entry tables */
1360 table = pInfo->htable;
1363 WORD count = *(WORD *)(ptr + table);
1364 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1365 for (; count > 0; count--, pEntry++)
1366 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1367 table = *(WORD *)pEntry;
1370 return (HLOCAL16)addr; /* Fixed block handle is addr */
1374 /***********************************************************************
1375 * LocalAlloc16 (KERNEL.5)
1377 HLOCAL16 LocalAlloc16( UINT16 flags, WORD size )
1379 return LOCAL_Alloc( CURRENT_DS, flags, size );
1383 /***********************************************************************
1384 * LocalReAlloc16 (KERNEL.6)
1386 HLOCAL16 LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1388 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1392 /***********************************************************************
1393 * LocalFree16 (KERNEL.7)
1395 HLOCAL16 LocalFree16( HLOCAL16 handle )
1397 return LOCAL_Free( CURRENT_DS, handle );
1401 /***********************************************************************
1402 * LocalLock16 (KERNEL.8)
1404 * Note: only the offset part of the pointer is returned by the relay code.
1406 SEGPTR LocalLock16( HLOCAL16 handle )
1408 HANDLE16 ds = CURRENT_DS;
1409 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1410 return PTR_SEG_OFF_TO_SEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1414 /***********************************************************************
1415 * LocalUnlock16 (KERNEL.9)
1417 BOOL16 LocalUnlock16( HLOCAL16 handle )
1419 return LOCAL_Unlock( CURRENT_DS, handle );
1423 /***********************************************************************
1424 * LocalSize16 (KERNEL.10)
1426 UINT16 LocalSize16( HLOCAL16 handle )
1428 return LOCAL_Size( CURRENT_DS, handle );
1432 /***********************************************************************
1433 * LocalHandle16 (KERNEL.11)
1435 HLOCAL16 LocalHandle16( WORD addr )
1437 return LOCAL_Handle( CURRENT_DS, addr );
1441 /***********************************************************************
1442 * LocalFlags16 (KERNEL.12)
1444 UINT16 LocalFlags16( HLOCAL16 handle )
1446 return LOCAL_Flags( CURRENT_DS, handle );
1450 /***********************************************************************
1451 * LocalCompact16 (KERNEL.13)
1453 UINT16 LocalCompact16( UINT16 minfree )
1455 dprintf_local( stddeb, "LocalCompact: %04x\n", minfree );
1456 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1460 /***********************************************************************
1461 * LocalNotify (KERNEL.14)
1463 FARPROC16 LocalNotify( FARPROC16 func )
1465 LOCALHEAPINFO *pInfo;
1466 FARPROC16 oldNotify;
1467 HANDLE16 ds = CURRENT_DS;
1469 if (!(pInfo = LOCAL_GetHeap( ds )))
1471 fprintf( stderr, "LOCAL_Notify(%04x): Local heap not found\n", ds );
1472 LOCAL_PrintHeap( ds );
1475 dprintf_local( stddeb, "LocalNotify(%04x): %08lx\n", ds, (DWORD)func );
1476 oldNotify = pInfo->notify;
1477 pInfo->notify = func;
1482 /***********************************************************************
1483 * LocalShrink16 (KERNEL.121)
1485 UINT16 LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1487 dprintf_local( stddeb, "LocalShrink: %04x %04x\n", handle, newsize );
1492 /***********************************************************************
1493 * GetHeapSpaces (KERNEL.138)
1495 DWORD GetHeapSpaces( HMODULE16 module )
1500 module = GetExePtr( module );
1501 if (!(pModule = MODULE_GetPtr( module ))) return 0;
1502 ds = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
1503 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1507 /***********************************************************************
1508 * LocalCountFree (KERNEL.161)
1510 WORD LocalCountFree(void)
1512 return LOCAL_CountFree( CURRENT_DS );
1516 /***********************************************************************
1517 * LocalHeapSize (KERNEL.162)
1519 WORD LocalHeapSize()
1521 dprintf_local( stddeb, "LocalHeapSize:\n" );
1522 return LOCAL_HeapSize( CURRENT_DS );
1526 /***********************************************************************
1527 * LocalHandleDelta (KERNEL.310)
1529 WORD LocalHandleDelta( WORD delta )
1531 LOCALHEAPINFO *pInfo;
1533 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1535 fprintf( stderr, "LocalHandleDelta: Local heap not found\n");
1536 LOCAL_PrintHeap( CURRENT_DS );
1539 if (delta) pInfo->hdelta = delta;
1540 dprintf_local(stddeb, "LocalHandleDelta: returning %04x\n", pInfo->hdelta);
1541 return pInfo->hdelta;
1545 /***********************************************************************
1546 * LocalInfo (TOOLHELP.56)
1548 BOOL16 LocalInfo( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1550 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(WIN16_GlobalLock16(handle)));
1551 if (!pInfo) return FALSE;
1552 pLocalInfo->wcItems = pInfo->items;
1557 /***********************************************************************
1558 * LocalFirst (TOOLHELP.57)
1560 BOOL16 LocalFirst( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1562 WORD ds = GlobalHandleToSel( handle );
1563 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1564 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1565 if (!pInfo) return FALSE;
1567 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1568 pLocalEntry->wAddress = pLocalEntry->hHandle;
1569 pLocalEntry->wFlags = LF_FIXED;
1570 pLocalEntry->wcLock = 0;
1571 pLocalEntry->wType = LT_NORMAL;
1572 pLocalEntry->hHeap = handle;
1573 pLocalEntry->wHeapType = NORMAL_HEAP;
1574 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1575 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1580 /***********************************************************************
1581 * LocalNext (TOOLHELP.58)
1583 BOOL16 LocalNext( LOCALENTRY *pLocalEntry )
1585 WORD ds = GlobalHandleToSel( pLocalEntry->hHeap );
1586 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1589 if (!LOCAL_GetHeap( ds )) return FALSE;
1590 if (!pLocalEntry->wNext) return FALSE;
1591 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1593 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1594 pLocalEntry->wAddress = pLocalEntry->hHandle;
1595 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1596 pLocalEntry->wcLock = 0;
1597 pLocalEntry->wType = LT_NORMAL;
1598 if (pArena->next != pLocalEntry->wNext) /* last one? */
1599 pLocalEntry->wNext = pArena->next;
1601 pLocalEntry->wNext = 0;
1602 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1608 #endif /* WINELIB */
1610 /***********************************************************************
1611 * LocalAlloc32 (KERNEL32.371)
1613 HLOCAL32 LocalAlloc32( UINT32 flags, DWORD size )
1615 return (HLOCAL32)GlobalAlloc32( flags, size );
1619 /***********************************************************************
1620 * LocalCompact32 (KERNEL32.372)
1622 UINT32 LocalCompact32( UINT32 minfree )
1624 return 0; /* LocalCompact does nothing in Win32 */
1628 /***********************************************************************
1629 * LocalFlags32 (KERNEL32.374)
1631 UINT32 LocalFlags32( HLOCAL32 handle )
1633 return GlobalFlags32( (HGLOBAL32)handle );
1637 /***********************************************************************
1638 * LocalFree32 (KERNEL32.375)
1640 HLOCAL32 LocalFree32( HLOCAL32 handle )
1642 return (HLOCAL32)GlobalFree32( (HGLOBAL32)handle );
1646 /***********************************************************************
1647 * LocalHandle32 (KERNEL32.376)
1649 HLOCAL32 LocalHandle32( LPCVOID ptr )
1651 return (HLOCAL32)GlobalHandle32( ptr );
1655 /***********************************************************************
1656 * LocalLock32 (KERNEL32.377)
1658 LPVOID LocalLock32( HLOCAL32 handle )
1660 return GlobalLock32( (HGLOBAL32)handle );
1664 /***********************************************************************
1665 * LocalReAlloc32 (KERNEL32.378)
1667 HLOCAL32 LocalReAlloc32( HLOCAL32 handle, DWORD size, UINT32 flags )
1669 return (HLOCAL32)GlobalReAlloc32( (HGLOBAL32)handle, size, flags );
1673 /***********************************************************************
1674 * LocalShrink32 (KERNEL32.379)
1676 UINT32 LocalShrink32( HGLOBAL32 handle, UINT32 newsize )
1678 return 0; /* LocalShrink does nothing in Win32 */
1682 /***********************************************************************
1683 * LocalSize32 (KERNEL32.380)
1685 UINT32 LocalSize32( HLOCAL32 handle )
1687 return GlobalSize32( (HGLOBAL32)handle );
1691 /***********************************************************************
1692 * LocalUnlock32 (KERNEL32.381)
1694 BOOL32 LocalUnlock32( HLOCAL32 handle )
1696 return GlobalUnlock32( (HGLOBAL32)handle );