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.
19 #include "wine/winbase16.h"
23 #include "stackframe.h"
25 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(local);
29 DECLARE_DEBUG_CHANNEL(heap);
34 WORD prev; /* Previous arena | arena type */
35 WORD next; /* Next arena */
36 /* Start of the memory block or free-list info */
37 WORD size; /* Size of the free block */
38 WORD free_prev; /* Previous free block */
39 WORD free_next; /* Next free block */
42 #define ARENA_HEADER_SIZE 4
43 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
45 /* Arena types (stored in 'prev' field of the arena) */
46 #define LOCAL_ARENA_FREE 0
47 #define LOCAL_ARENA_FIXED 1
49 /* LocalNotify() msgs */
55 /* Layout of a handle entry table
57 * WORD count of entries
58 * LOCALHANDLEENTRY[count] entries
59 * WORD near ptr to next table
63 WORD addr; /* Address of the MOVEABLE block */
64 BYTE flags; /* Flags for this block */
65 BYTE lock; /* Lock count */
69 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
70 * in case something actually relies on this.
71 * Note that if the architecture does not allow unaligned accesses, we make
72 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
74 * An unused handle has lock = flags = 0xff. In windows addr is that of next
75 * free handle, at the moment in wine we set it to 0.
77 * A discarded block's handle has lock = addr = 0 and flags = 0x40
78 * (LMEM_DISCARDED >> 8)
81 #ifdef ALLOW_UNALIGNED_ACCESS
82 #define MOVEABLE_PREFIX sizeof(HLOCAL16)
84 #define MOVEABLE_PREFIX sizeof(int)
92 WORD check; /* 00 Heap checking flag */
93 WORD freeze; /* 02 Heap frozen flag */
94 WORD items; /* 04 Count of items on the heap */
95 WORD first; /* 06 First item of the heap */
96 WORD pad1; /* 08 Always 0 */
97 WORD last; /* 0a Last item of the heap */
98 WORD pad2; /* 0c Always 0 */
99 BYTE ncompact; /* 0e Compactions counter */
100 BYTE dislevel; /* 0f Discard level */
101 DWORD distotal; /* 10 Total bytes discarded */
102 WORD htable; /* 14 Pointer to handle table */
103 WORD hfree; /* 16 Pointer to free handle table */
104 WORD hdelta; /* 18 Delta to expand the handle table */
105 WORD expand; /* 1a Pointer to expand function (unused) */
106 WORD pstat; /* 1c Pointer to status structure (unused) */
107 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
108 WORD lock; /* 22 Lock count for the heap */
109 WORD extra; /* 24 Extra bytes to allocate when expanding */
110 WORD minsize; /* 26 Minimum size of the heap */
111 WORD magic; /* 28 Magic number */
116 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
118 /* All local heap allocations are aligned on 4-byte boundaries */
119 #define LALIGN(word) (((word) + 3) & ~3)
121 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
122 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
123 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
124 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
126 /* determine whether the handle belongs to a fixed or a moveable block */
127 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
128 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
131 /* ### start build ### */
132 extern WORD CALLBACK LOCAL_CallTo16_word_www(FARPROC16,WORD,HLOCAL16,WORD);
133 /* ### stop build ### */
135 /***********************************************************************
138 * Return a pointer to the local heap, making sure it exists.
140 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
142 LOCALHEAPINFO *pInfo;
143 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
144 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
145 if (!ptr || !ptr->heap) return NULL;
146 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
148 WARN("Bad pointer\n");
151 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
152 if (pInfo->magic != LOCAL_HEAP_MAGIC)
161 /***********************************************************************
162 * LOCAL_MakeBlockFree
164 * Make a block free, inserting it in the free-list.
165 * 'block' is the handle of the block arena; 'baseptr' points to
166 * the beginning of the data segment containing the heap.
168 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
170 LOCALARENA *pArena, *pNext;
173 /* Mark the block as free */
175 pArena = ARENA_PTR( baseptr, block );
176 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
177 pArena->size = pArena->next - block;
179 /* Find the next free block (last block is always free) */
184 pNext = ARENA_PTR( baseptr, next );
185 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
189 TRACE("%04x, next %04x\n", block, next );
190 /* Insert the free block in the free-list */
192 pArena->free_prev = pNext->free_prev;
193 pArena->free_next = next;
194 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
195 pNext->free_prev = block;
199 /***********************************************************************
200 * LOCAL_RemoveFreeBlock
202 * Remove a block from the free-list.
203 * 'block' is the handle of the block arena; 'baseptr' points to
204 * the beginning of the data segment containing the heap.
206 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
208 /* Mark the block as fixed */
210 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
211 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
213 /* Remove it from the list */
215 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
216 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
220 /***********************************************************************
223 * Insert a new block in the heap.
224 * 'new' is the handle of the new block arena; 'baseptr' points to
225 * the beginning of the data segment containing the heap; 'prev' is
226 * the block before the new one.
228 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
230 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
231 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
233 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
234 pNew->next = pPrev->next;
235 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
236 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
241 /***********************************************************************
244 * Remove a block from the heap.
245 * 'block' is the handle of the block arena; 'baseptr' points to
246 * the beginning of the data segment containing the heap.
248 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
250 LOCALARENA *pArena, *pTmp;
252 /* Remove the block from the free-list */
255 pArena = ARENA_PTR( baseptr, block );
256 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
257 LOCAL_RemoveFreeBlock( baseptr, block );
259 /* If the previous block is free, expand its size */
261 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
262 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
263 pTmp->size += pArena->next - block;
265 /* Remove the block from the linked list */
267 pTmp->next = pArena->next;
268 pTmp = ARENA_PTR( baseptr, pArena->next );
269 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
273 /***********************************************************************
276 static void LOCAL_PrintHeap( HANDLE16 ds )
279 LOCALHEAPINFO *pInfo;
282 /* FIXME - the test should be done when calling the function!
283 plus is not clear that we should print this info
284 only when TRACE_ON is on! */
285 if(!TRACE_ON(local)) return;
287 ptr = MapSL( MAKESEGPTR( ds, 0 ));
288 pInfo = LOCAL_GetHeap( ds );
292 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
295 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
296 ds, pInfo->first, pInfo->last, pInfo->items );
298 arena = pInfo->first;
301 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
302 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
303 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
304 if (arena == pInfo->first)
306 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
307 pArena->size, pArena->free_prev, pArena->free_next );
309 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
311 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
312 pArena->size, pArena->free_prev, pArena->free_next );
313 if (pArena->next == arena) break; /* last one */
314 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
316 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
320 if (pArena->next == arena)
322 DPRINTF( "*** last block is not marked free\n" );
325 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
327 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
328 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
331 arena = pArena->next;
336 /***********************************************************************
337 * LocalInit (KERNEL.4)
339 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
342 WORD heapInfoArena, freeArena, lastArena;
343 LOCALHEAPINFO *pHeapInfo;
344 LOCALARENA *pArena, *pFirstArena, *pLastArena;
348 /* The initial layout of the heap is: */
349 /* - first arena (FIXED) */
350 /* - heap info structure (FIXED) */
351 /* - large free block (FREE) */
352 /* - last arena (FREE) */
354 TRACE("%04x %04x-%04x\n", selector, start, end);
355 if (!selector) selector = CURRENT_DS;
359 /* If TRACE_ON(heap) is set, the global heap blocks are */
360 /* cleared before use, so we can test for double initialization. */
361 if (LOCAL_GetHeap(selector))
363 ERR("Heap %04x initialized twice.\n", selector);
364 LOCAL_PrintHeap(selector);
370 /* start == 0 means: put the local heap at the end of the segment */
372 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
373 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
374 if ( end > 0xfffe ) end = 0xfffe;
380 if ((pModule = NE_GetPtr( GlobalHandle16( selector ) )))
382 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
385 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
386 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
389 if ( segNr < pModule->seg_count )
391 WORD minsize = pSeg->minsize;
392 if ( pModule->ss == segNr+1 )
393 minsize += pModule->stack_size;
395 TRACE(" new start %04x, minstart: %04x\n", start, minsize);
399 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
401 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
402 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
403 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
404 + sizeof(LOCALHEAPINFO) );
405 lastArena = (end - sizeof(LOCALARENA)) & ~3;
407 /* Make sure there's enough space. */
409 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
411 /* Initialise the first arena */
413 pFirstArena = ARENA_PTR( ptr, start );
414 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
415 pFirstArena->next = heapInfoArena;
416 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
417 pFirstArena->free_prev = start; /* this one */
418 pFirstArena->free_next = freeArena;
420 /* Initialise the arena of the heap info structure */
422 pArena = ARENA_PTR( ptr, heapInfoArena );
423 pArena->prev = start | LOCAL_ARENA_FIXED;
424 pArena->next = freeArena;
426 /* Initialise the heap info structure */
428 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
429 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
430 pHeapInfo->items = 4;
431 pHeapInfo->first = start;
432 pHeapInfo->last = lastArena;
433 pHeapInfo->htable = 0;
434 pHeapInfo->hdelta = 0x20;
435 pHeapInfo->extra = 0x200;
436 pHeapInfo->minsize = lastArena - freeArena;
437 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
439 /* Initialise the large free block */
441 pArena = ARENA_PTR( ptr, freeArena );
442 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
443 pArena->next = lastArena;
444 pArena->size = lastArena - freeArena;
445 pArena->free_prev = start;
446 pArena->free_next = lastArena;
448 /* Initialise the last block */
450 pLastArena = ARENA_PTR( ptr, lastArena );
451 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
452 pLastArena->next = lastArena; /* this one */
453 pLastArena->size = LALIGN(sizeof(LOCALARENA));
454 pLastArena->free_prev = freeArena;
455 pLastArena->free_next = lastArena; /* this one */
457 /* Store the local heap address in the instance data */
459 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
460 LOCAL_PrintHeap( selector );
464 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
468 /***********************************************************************
471 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
476 LOCALHEAPINFO *pHeapInfo;
477 WORD freeArena, lastArena;
478 LOCALARENA *pArena, *pLastArena;
481 hseg = GlobalHandle16( ds );
482 /* maybe mem allocated by Virtual*() ? */
483 if (!hseg) return FALSE;
485 oldsize = GlobalSize16( hseg );
486 /* if nothing can be gained, return */
487 if (oldsize > 0xfff0) return FALSE;
488 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
489 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
490 pHeapInfo = LOCAL_GetHeap( ds );
491 if (pHeapInfo == NULL) {
492 ERR("Heap not found\n" );
495 end = GlobalSize16( hseg );
496 lastArena = (end - sizeof(LOCALARENA)) & ~3;
498 /* Update the HeapInfo */
500 freeArena = pHeapInfo->last;
501 pHeapInfo->last = lastArena;
502 pHeapInfo->minsize += end - oldsize;
504 /* grow the old last block */
505 pArena = ARENA_PTR( ptr, freeArena );
506 pArena->size = lastArena - freeArena;
507 pArena->next = lastArena;
508 pArena->free_next = lastArena;
510 /* Initialise the new last block */
512 pLastArena = ARENA_PTR( ptr, lastArena );
513 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
514 pLastArena->next = lastArena; /* this one */
515 pLastArena->size = LALIGN(sizeof(LOCALARENA));
516 pLastArena->free_prev = freeArena;
517 pLastArena->free_next = lastArena; /* this one */
519 /* If block before freeArena is also free then merge them */
520 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
522 LOCAL_RemoveBlock(ptr, freeArena);
526 TRACE("Heap expanded\n" );
527 LOCAL_PrintHeap( ds );
532 /***********************************************************************
535 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
537 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
538 LOCALHEAPINFO *pInfo;
539 LOCALARENA *pArena, *pPrev, *pNext;
541 TRACE("%04x ds=%04x\n", arena, ds );
542 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
544 pArena = ARENA_PTR( ptr, arena );
545 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
547 /* shouldn't happen */
548 ERR("Trying to free block %04x twice!\n",
550 LOCAL_PrintHeap( ds );
554 /* Check if we can merge with the previous block */
556 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
557 pNext = ARENA_PTR( ptr, pArena->next );
558 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
560 arena = pArena->prev & ~3;
562 LOCAL_RemoveBlock( ptr, pPrev->next );
565 else /* Make a new free block */
567 LOCAL_MakeBlockFree( ptr, arena );
570 /* Check if we can merge with the next block */
572 if ((pArena->next == pArena->free_next) &&
573 (pArena->next != pInfo->last))
575 LOCAL_RemoveBlock( ptr, pArena->next );
582 /***********************************************************************
585 * Shrink an arena by creating a free block at its end if possible.
586 * 'size' includes the arena header, and must be aligned.
588 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
590 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
591 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
593 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
595 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
597 LOCAL_AddBlock( ptr, arena, arena + size );
599 LOCAL_FreeArena( ds, arena + size );
604 /***********************************************************************
605 * LOCAL_GrowArenaDownward
607 * Grow an arena downward by using the previous arena (must be free).
609 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
611 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
612 LOCALHEAPINFO *pInfo;
613 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
614 WORD prevArena = pArena->prev & ~3;
615 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
619 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
620 offset = pPrevArena->size;
621 size = pArena->next - arena - ARENA_HEADER_SIZE;
622 LOCAL_RemoveFreeBlock( ptr, prevArena );
623 LOCAL_RemoveBlock( ptr, arena );
625 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
626 while (offset < size)
628 memcpy( p, p + offset, offset );
632 if (size) memcpy( p, p + offset, size );
633 LOCAL_ShrinkArena( ds, prevArena, newsize );
638 /***********************************************************************
639 * LOCAL_GrowArenaUpward
641 * Grow an arena upward by using the next arena (must be free and big
642 * enough). Newsize includes the arena header and must be aligned.
644 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
646 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
647 LOCALHEAPINFO *pInfo;
648 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
649 WORD nextArena = pArena->next;
651 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
652 LOCAL_RemoveBlock( ptr, nextArena );
654 LOCAL_ShrinkArena( ds, arena, newsize );
658 /***********************************************************************
661 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
663 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
664 LOCALHEAPINFO *pInfo;
669 if (!(pInfo = LOCAL_GetHeap( ds )))
671 ERR("Local heap not found\n" );
675 arena = pInfo->first;
676 pArena = ARENA_PTR( ptr, arena );
677 while (arena != pArena->free_next)
679 arena = pArena->free_next;
680 pArena = ARENA_PTR( ptr, arena );
681 if (pArena->size >= freespace) freespace = pArena->size;
683 /* FIXME doesn't yet calculate space that would become free if everything
684 were discarded when countdiscard == 1 */
685 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
686 else freespace -= ARENA_HEADER_SIZE;
691 /***********************************************************************
694 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
696 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
697 LOCALHEAPINFO *pInfo;
698 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
699 WORD arena, movearena, finalarena, table;
700 WORD count, movesize, size;
702 LOCALHANDLEENTRY *pEntry;
704 if (!(pInfo = LOCAL_GetHeap( ds )))
706 ERR("Local heap not found\n" );
710 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
712 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
713 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
715 TRACE("Returning %04x.\n", freespace);
718 TRACE("Compacting heap %04x.\n", ds);
719 table = pInfo->htable;
722 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
723 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
725 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
727 /* OK we can move this one if we want */
728 TRACE("handle %04x (block %04x) can be moved.\n",
729 (WORD)((char *)pEntry - ptr), pEntry->addr);
730 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
731 pMoveArena = ARENA_PTR(ptr, movearena);
732 movesize = pMoveArena->next - movearena;
733 arena = pInfo->first;
734 pArena = ARENA_PTR(ptr, arena);
737 /* Try to find the smallest arena that will do, */
738 /* which is below us in memory */
741 arena = pArena->free_next;
742 pArena = ARENA_PTR(ptr, arena);
743 if(arena >= movearena)
745 if(arena == pArena->free_next)
747 if((pArena->size >= movesize) && (pArena->size < size))
753 if (finalarena) /* Actually got somewhere to move */
755 TRACE("Moving it to %04x.\n", finalarena);
756 pFinalArena = ARENA_PTR(ptr, finalarena);
757 size = pFinalArena->size;
758 LOCAL_RemoveFreeBlock(ptr, finalarena);
759 LOCAL_ShrinkArena( ds, finalarena, movesize );
760 /* Copy the arena to it's new location */
761 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
762 (char *)pMoveArena + ARENA_HEADER_SIZE,
763 movesize - ARENA_HEADER_SIZE );
764 /* Free the old location */
765 LOCAL_FreeArena(ds, movearena);
767 LOCAL_CallTo16_word_www(pInfo->notify, LN_MOVE,
768 (WORD)((char *)pEntry - ptr), pEntry->addr);
769 /* Update handle table entry */
770 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
772 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
775 /* Previous arena is free (but < movesize) */
776 /* so we can 'slide' movearena down into it */
777 finalarena = pMoveArena->prev & ~3;
778 LOCAL_GrowArenaDownward( ds, movearena, movesize );
779 /* Update handle table entry */
780 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
784 table = *(WORD *)pEntry;
786 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
787 if(freespace >= minfree || (flags & LMEM_NODISCARD))
789 TRACE("Returning %04x.\n", freespace);
793 table = pInfo->htable;
796 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
797 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
799 if(pEntry->addr && pEntry->lock == 0 &&
800 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
802 TRACE("Discarding handle %04x (block %04x).\n",
803 (char *)pEntry - ptr, pEntry->addr);
804 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
806 LOCAL_CallTo16_word_www(pInfo->notify, LN_DISCARD,
807 (char *)pEntry - ptr, pEntry->flags);
809 pEntry->flags = (LMEM_DISCARDED >> 8);
812 table = *(WORD *)pEntry;
814 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
818 /***********************************************************************
819 * LOCAL_FindFreeBlock
821 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
823 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
824 LOCALHEAPINFO *pInfo;
828 if (!(pInfo = LOCAL_GetHeap( ds )))
830 ERR("Local heap not found\n" );
835 arena = pInfo->first;
836 pArena = ARENA_PTR( ptr, arena );
838 arena = pArena->free_next;
839 pArena = ARENA_PTR( ptr, arena );
840 if (arena == pArena->free_next) break;
841 if (pArena->size >= size) return arena;
843 TRACE("not enough space\n" );
849 /***********************************************************************
852 static const char *get_heap_name( WORD ds )
854 HINSTANCE16 inst = LoadLibrary16( "GDI" );
855 if (ds == GlobalHandleToSel16( inst ))
857 FreeLibrary16( inst );
860 FreeLibrary16( inst );
861 inst = LoadLibrary16( "USER" );
862 if (ds == GlobalHandleToSel16( inst ))
864 FreeLibrary16( inst );
867 FreeLibrary16( inst );
871 /***********************************************************************
873 * The segment may get moved around in this function, so all callers
874 * should reset their pointer variables.
876 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
878 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
879 LOCALHEAPINFO *pInfo;
883 if (!(pInfo = LOCAL_GetHeap( ds )))
885 ERR("Local heap not found\n");
890 size += ARENA_HEADER_SIZE;
891 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
896 /* Find a suitable free block */
897 arena = LOCAL_FindFreeBlock( ds, size );
899 /* no space: try to make some */
900 LOCAL_Compact( ds, size, flags );
901 arena = LOCAL_FindFreeBlock( ds, size );
904 /* still no space: try to grow the segment */
905 if (!(LOCAL_GrowHeap( ds )))
908 /* FIXME: doesn't work correctly yet */
909 if ((pInfo->notify) && (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds - 20, size))) /* FIXME: "size" correct ? (should indicate bytes needed) */
914 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
915 pInfo = LOCAL_GetHeap( ds );
916 arena = LOCAL_FindFreeBlock( ds, size );
919 ERR( "not enough space in %s heap %04x for %d bytes\n",
920 get_heap_name(ds), ds, size );
922 if ((pInfo->notify) &&
923 /* FIXME: "size" correct ? (should indicate bytes needed) */
924 (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds, size)))
930 /* Make a block out of the free arena */
931 pArena = ARENA_PTR( ptr, arena );
932 TRACE("size = %04x, arena %04x size %04x\n",
933 size, arena, pArena->size );
934 LOCAL_RemoveFreeBlock( ptr, arena );
935 LOCAL_ShrinkArena( ds, arena, size );
937 if (flags & LMEM_ZEROINIT)
938 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
939 return arena + ARENA_HEADER_SIZE;
943 /***********************************************************************
946 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
948 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
949 LOCALHEAPINFO *pInfo;
950 LOCALHANDLEENTRY *pEntry;
955 if (!(pInfo = LOCAL_GetHeap( ds )))
957 ERR("Local heap not found\n");
962 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
963 + 2 * sizeof(WORD), LMEM_FIXED )))
965 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
966 ERR("ptr == NULL after GetBlock.\n");
967 if (!(pInfo = LOCAL_GetHeap( ds )))
968 ERR("pInfo == NULL after GetBlock.\n");
970 /* Fill the entry table */
972 *(WORD *)(ptr + handle) = pInfo->hdelta;
973 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
974 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
975 pEntry->lock = pEntry->flags = 0xff;
978 *(WORD *)pEntry = pInfo->htable;
979 pInfo->htable = handle;
984 /***********************************************************************
985 * LOCAL_GetNewHandleEntry
987 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
989 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
990 LOCALHEAPINFO *pInfo;
991 LOCALHANDLEENTRY *pEntry = NULL;
994 if (!(pInfo = LOCAL_GetHeap( ds )))
996 ERR("Local heap not found\n");
1001 /* Find a free slot in existing tables */
1003 table = pInfo->htable;
1006 WORD count = *(WORD *)(ptr + table);
1007 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1008 for (; count > 0; count--, pEntry++)
1009 if (pEntry->lock == 0xff) break;
1011 table = *(WORD *)pEntry;
1014 if (!table) /* We need to create a new table */
1016 if (!LOCAL_NewHTable( ds )) return 0;
1017 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1018 pInfo = LOCAL_GetHeap( ds );
1019 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1022 /* Now allocate this entry */
1026 TRACE("(%04x): %04x\n",
1027 ds, ((char *)pEntry - ptr) );
1028 return (HLOCAL16)((char *)pEntry - ptr);
1032 /***********************************************************************
1033 * LOCAL_FreeHandleEntry
1035 * Free a handle table entry.
1037 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1039 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1040 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1041 LOCALHEAPINFO *pInfo;
1043 WORD table, count, i;
1045 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1047 /* Find the table where this handle comes from */
1049 pTable = &pInfo->htable;
1052 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1053 if ((handle >= *pTable + sizeof(WORD)) &&
1054 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1055 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1059 ERR("Invalid entry %04x\n", handle);
1060 LOCAL_PrintHeap( ds );
1064 /* Make the entry free */
1066 pEntry->addr = 0; /* just in case */
1067 pEntry->lock = 0xff;
1068 pEntry->flags = 0xff;
1069 /* Now check if all entries in this table are free */
1072 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1073 count = *(WORD *)(ptr + table);
1074 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1076 /* Remove the table from the linked list and free it */
1078 TRACE("(%04x): freeing table %04x\n",
1080 *pTable = *(WORD *)pEntry;
1081 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1085 /***********************************************************************
1088 * Implementation of LocalFree().
1090 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1092 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1094 TRACE("%04x ds=%04x\n", handle, ds );
1096 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1097 if (HANDLE_FIXED( handle ))
1099 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1100 else return handle; /* couldn't free it */
1104 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1105 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1107 TRACE("real block at %04x\n",
1109 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1110 return handle; /* couldn't free it */
1112 LOCAL_FreeHandleEntry( ds, handle );
1118 /***********************************************************************
1121 * Implementation of LocalAlloc().
1124 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1129 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1131 if(size > 0 && size <= 4) size = 5;
1132 if (flags & LMEM_MOVEABLE)
1134 LOCALHANDLEENTRY *plhe;
1139 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1142 else /* We just need to allocate a discarded handle */
1144 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1146 WARN("Couldn't get handle.\n");
1148 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1151 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1152 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1156 plhe->addr = hmem + MOVEABLE_PREFIX;
1157 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1158 *(HLOCAL16 *)(ptr + hmem) = handle;
1163 plhe->flags = LMEM_DISCARDED >> 8;
1170 handle = LOCAL_GetBlock( ds, size, flags );
1176 /***********************************************************************
1179 * Implementation of LocalReAlloc().
1181 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1183 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1184 LOCALHEAPINFO *pInfo;
1185 LOCALARENA *pArena, *pNext;
1186 LOCALHANDLEENTRY *pEntry = NULL;
1187 WORD arena, oldsize;
1188 HLOCAL16 hmem, blockhandle;
1191 if (!handle) return 0;
1192 if(HANDLE_MOVEABLE(handle) &&
1193 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1196 TRACE("%04x %d %04x ds=%04x\n",
1197 handle, size, flags, ds );
1198 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1200 if (HANDLE_FIXED( handle ))
1201 blockhandle = handle;
1204 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1205 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1209 WARN("Dicarded block has non-zero addr.\n");
1210 TRACE("ReAllocating discarded block\n");
1211 if(size <= 4) size = 5;
1212 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1214 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1215 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1216 pEntry->addr = hl + MOVEABLE_PREFIX;
1219 *(HLOCAL16 *)(ptr + hl) = handle;
1222 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1224 ERR("(%04x,%04x): invalid handle\n",
1228 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1229 ERR("Back ptr to handle is invalid\n");
1234 if (flags & LMEM_MODIFY)
1236 if (HANDLE_MOVEABLE(handle))
1238 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1239 pEntry->flags = (flags & 0x0f00) >> 8;
1240 TRACE("Changing flags to %x.\n", pEntry->flags);
1247 if (flags & LMEM_MOVEABLE)
1249 if (HANDLE_FIXED(handle))
1251 TRACE("Freeing fixed block.\n");
1252 return LOCAL_Free( ds, handle );
1254 else /* Moveable block */
1256 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1257 if (pEntry->lock == 0)
1259 /* discards moveable blocks */
1260 TRACE("Discarding block\n");
1261 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1263 pEntry->flags = (LMEM_DISCARDED >> 8);
1271 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1272 if (pEntry->lock == 0)
1275 return LOCAL_Free( ds, handle );
1281 arena = ARENA_HEADER( blockhandle );
1282 TRACE("arena is %04x\n", arena );
1283 pArena = ARENA_PTR( ptr, arena );
1285 if(size <= 4) size = 5;
1286 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1287 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1288 nextarena = LALIGN(blockhandle + size);
1290 /* Check for size reduction */
1292 if (nextarena <= pArena->next)
1294 TRACE("size reduction, making new free block\n");
1295 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1296 TRACE("returning %04x\n", handle );
1300 /* Check if the next block is free and large enough */
1302 pNext = ARENA_PTR( ptr, pArena->next );
1303 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1304 (nextarena <= pNext->next))
1306 TRACE("size increase, making new free block\n");
1307 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1308 TRACE("returning %04x\n", handle );
1312 /* Now we have to allocate a new block, but not if (fixed block or locked
1313 block) and no LMEM_MOVEABLE */
1315 if (!(flags & LMEM_MOVEABLE))
1317 if (HANDLE_FIXED(handle))
1319 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1324 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1326 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1332 hmem = LOCAL_GetBlock( ds, size, flags );
1333 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1334 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1335 { /* a compaction, which might in turn have */
1336 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1337 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1341 /* Remove the block from the heap and try again */
1342 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1343 if (!buffer) return 0;
1344 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1345 LOCAL_FreeArena( ds, arena );
1346 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1348 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1350 ERR("Can't restore saved block\n" );
1351 HeapFree( GetProcessHeap(), 0, buffer );
1356 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1357 memcpy( ptr + hmem, buffer, oldsize );
1358 HeapFree( GetProcessHeap(), 0, buffer );
1362 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1363 LOCAL_FreeArena( ds, arena );
1365 if (HANDLE_MOVEABLE( handle ))
1367 TRACE("fixing handle\n");
1368 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1369 pEntry->addr = hmem + MOVEABLE_PREFIX;
1370 /* Back ptr should still be correct */
1371 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1372 ERR("back ptr is invalid.\n");
1375 if (size == oldsize) hmem = 0; /* Realloc failed */
1376 TRACE("returning %04x\n", hmem );
1381 /***********************************************************************
1382 * LOCAL_InternalLock
1384 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1386 HLOCAL16 old_handle = handle;
1388 if (HANDLE_MOVEABLE(handle))
1390 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1391 if (pEntry->flags == LMEM_DISCARDED) return 0;
1392 if (pEntry->lock < 0xfe) pEntry->lock++;
1393 handle = pEntry->addr;
1395 TRACE("%04x returning %04x\n",
1396 old_handle, handle );
1401 /***********************************************************************
1404 void *LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1406 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1407 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1411 /***********************************************************************
1414 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1416 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1418 TRACE("%04x\n", handle );
1419 if (HANDLE_MOVEABLE(handle))
1421 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1422 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1423 /* For moveable block, return the new lock count */
1424 /* (see _Windows_Internals_ p. 197) */
1425 return --pEntry->lock;
1431 /***********************************************************************
1434 * Implementation of LocalSize().
1436 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1438 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1441 TRACE("%04x ds=%04x\n", handle, ds );
1443 if (!handle) return 0;
1444 if (HANDLE_MOVEABLE( handle ))
1446 handle = *(WORD *)(ptr + handle);
1447 if (!handle) return 0;
1448 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1451 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1453 return pArena->next - handle;
1457 /***********************************************************************
1460 * Implementation of LocalFlags().
1462 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1464 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1466 if (HANDLE_MOVEABLE(handle))
1468 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1469 TRACE("(%04x,%04x): returning %04x\n",
1470 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1471 return pEntry->lock | (pEntry->flags << 8);
1475 TRACE("(%04x,%04x): returning 0\n",
1482 /***********************************************************************
1485 * Implementation of LocalHeapSize().
1487 WORD LOCAL_HeapSize( HANDLE16 ds )
1489 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1490 if (!pInfo) return 0;
1491 return pInfo->last - pInfo->first;
1495 /***********************************************************************
1498 * Implementation of LocalCountFree().
1500 WORD LOCAL_CountFree( HANDLE16 ds )
1504 LOCALHEAPINFO *pInfo;
1505 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1507 if (!(pInfo = LOCAL_GetHeap( ds )))
1509 ERR("(%04x): Local heap not found\n", ds );
1510 LOCAL_PrintHeap( ds );
1515 arena = pInfo->first;
1516 pArena = ARENA_PTR( ptr, arena );
1519 arena = pArena->free_next;
1520 pArena = ARENA_PTR( ptr, arena );
1521 if (arena == pArena->free_next) break;
1522 total += pArena->size;
1524 TRACE("(%04x): returning %d\n", ds, total);
1529 /***********************************************************************
1532 * Implementation of LocalHandle().
1534 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1536 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1537 LOCALHEAPINFO *pInfo;
1540 if (!(pInfo = LOCAL_GetHeap( ds )))
1542 ERR("(%04x): Local heap not found\n", ds );
1543 LOCAL_PrintHeap( ds );
1547 /* Find the address in the entry tables */
1549 table = pInfo->htable;
1552 WORD count = *(WORD *)(ptr + table);
1553 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1554 for (; count > 0; count--, pEntry++)
1555 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1556 table = *(WORD *)pEntry;
1559 return (HLOCAL16)addr; /* Fixed block handle is addr */
1563 /***********************************************************************
1564 * LocalAlloc (KERNEL.5)
1566 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1568 HLOCAL16 ret = LOCAL_Alloc( CURRENT_DS, flags, size );
1569 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
1574 /***********************************************************************
1575 * LocalReAlloc (KERNEL.6)
1577 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1579 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1583 /***********************************************************************
1584 * LocalFree (KERNEL.7)
1586 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1588 return LOCAL_Free( CURRENT_DS, handle );
1592 /***********************************************************************
1593 * LocalLock (KERNEL.8)
1595 * Note: only the offset part of the pointer is returned by the relay code.
1597 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1599 WORD ds = CURRENT_DS;
1600 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1601 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1605 /***********************************************************************
1606 * LocalUnlock (KERNEL.9)
1608 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1610 return LOCAL_Unlock( CURRENT_DS, handle );
1614 /***********************************************************************
1615 * LocalSize (KERNEL.10)
1617 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1619 return LOCAL_Size( CURRENT_DS, handle );
1623 /***********************************************************************
1624 * LocalHandle (KERNEL.11)
1626 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1628 return LOCAL_Handle( CURRENT_DS, addr );
1632 /***********************************************************************
1633 * LocalFlags (KERNEL.12)
1635 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1637 return LOCAL_Flags( CURRENT_DS, handle );
1641 /***********************************************************************
1642 * LocalCompact (KERNEL.13)
1644 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1646 TRACE("%04x\n", minfree );
1647 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1651 /***********************************************************************
1652 * LocalNotify (KERNEL.14)
1654 * Installs a callback function that is called for local memory events
1655 * Callback function prototype is
1656 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1659 * NotifyFunc seems to be responsible for allocating some memory,
1660 * returns TRUE for success.
1661 * wArg = number of bytes needed additionally
1663 * hMem = handle; wArg = old mem location
1665 * NotifyFunc seems to be strongly encouraged to return TRUE,
1666 * otherwise LogError() gets called.
1667 * hMem = handle; wArg = flags
1669 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1671 LOCALHEAPINFO *pInfo;
1672 FARPROC16 oldNotify;
1673 HANDLE16 ds = CURRENT_DS;
1675 if (!(pInfo = LOCAL_GetHeap( ds )))
1677 ERR("(%04x): Local heap not found\n", ds );
1678 LOCAL_PrintHeap( ds );
1681 TRACE("(%04x): %08lx\n", ds, (DWORD)func );
1682 FIXME("Half implemented\n");
1683 oldNotify = pInfo->notify;
1684 pInfo->notify = func;
1689 /***********************************************************************
1690 * LocalShrink (KERNEL.121)
1692 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1694 TRACE("%04x %04x\n", handle, newsize );
1699 /***********************************************************************
1700 * GetHeapSpaces (KERNEL.138)
1702 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1707 if (!(pModule = NE_GetPtr( module ))) return 0;
1709 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1710 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1714 /***********************************************************************
1715 * LocalCountFree (KERNEL.161)
1717 WORD WINAPI LocalCountFree16(void)
1719 return LOCAL_CountFree( CURRENT_DS );
1723 /***********************************************************************
1724 * LocalHeapSize (KERNEL.162)
1726 WORD WINAPI LocalHeapSize16(void)
1729 return LOCAL_HeapSize( CURRENT_DS );
1733 /***********************************************************************
1734 * LocalHandleDelta (KERNEL.310)
1736 WORD WINAPI LocalHandleDelta16( WORD delta )
1738 LOCALHEAPINFO *pInfo;
1740 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1742 ERR("Local heap not found\n");
1743 LOCAL_PrintHeap( CURRENT_DS );
1746 if (delta) pInfo->hdelta = delta;
1747 TRACE("returning %04x\n", pInfo->hdelta);
1748 return pInfo->hdelta;
1752 /***********************************************************************
1753 * LocalInfo (TOOLHELP.56)
1755 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1757 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle)));
1758 if (!pInfo) return FALSE;
1759 pLocalInfo->wcItems = pInfo->items;
1764 /***********************************************************************
1765 * LocalFirst (TOOLHELP.57)
1767 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1769 WORD ds = GlobalHandleToSel16( handle );
1770 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1771 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1772 if (!pInfo) return FALSE;
1774 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1775 pLocalEntry->wAddress = pLocalEntry->hHandle;
1776 pLocalEntry->wFlags = LF_FIXED;
1777 pLocalEntry->wcLock = 0;
1778 pLocalEntry->wType = LT_NORMAL;
1779 pLocalEntry->hHeap = handle;
1780 pLocalEntry->wHeapType = NORMAL_HEAP;
1781 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1782 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1787 /***********************************************************************
1788 * LocalNext (TOOLHELP.58)
1790 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1792 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1793 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1796 if (!LOCAL_GetHeap( ds )) return FALSE;
1797 if (!pLocalEntry->wNext) return FALSE;
1798 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1800 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1801 pLocalEntry->wAddress = pLocalEntry->hHandle;
1802 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1803 pLocalEntry->wcLock = 0;
1804 pLocalEntry->wType = LT_NORMAL;
1805 if (pArena->next != pLocalEntry->wNext) /* last one? */
1806 pLocalEntry->wNext = pArena->next;
1808 pLocalEntry->wNext = 0;
1809 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1814 /***********************************************************************
1815 * LocalAlloc (KERNEL32.@)
1820 HLOCAL WINAPI LocalAlloc(
1821 UINT flags, /* [in] Allocation attributes */
1822 DWORD size /* [in] Number of bytes to allocate */
1824 return (HLOCAL)GlobalAlloc( flags, size );
1828 /***********************************************************************
1829 * LocalCompact (KERNEL32.@)
1831 UINT WINAPI LocalCompact( UINT minfree )
1833 return 0; /* LocalCompact does nothing in Win32 */
1837 /***********************************************************************
1838 * LocalFlags (KERNEL32.@)
1840 * Value specifying allocation flags and lock count.
1841 * LMEM_INVALID_HANDLE: Failure
1843 UINT WINAPI LocalFlags(
1844 HLOCAL handle /* [in] Handle of memory object */
1846 return GlobalFlags( (HGLOBAL)handle );
1850 /***********************************************************************
1851 * LocalFree (KERNEL32.@)
1856 HLOCAL WINAPI LocalFree(
1857 HLOCAL handle /* [in] Handle of memory object */
1859 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
1863 /***********************************************************************
1864 * LocalHandle (KERNEL32.@)
1869 HLOCAL WINAPI LocalHandle(
1870 LPCVOID ptr /* [in] Address of local memory object */
1872 return (HLOCAL)GlobalHandle( ptr );
1876 /***********************************************************************
1877 * LocalLock (KERNEL32.@)
1878 * Locks a local memory object and returns pointer to the first byte
1879 * of the memory block.
1885 LPVOID WINAPI LocalLock(
1886 HLOCAL handle /* [in] Address of local memory object */
1888 return GlobalLock( (HGLOBAL)handle );
1892 /***********************************************************************
1893 * LocalReAlloc (KERNEL32.@)
1898 HLOCAL WINAPI LocalReAlloc(
1899 HLOCAL handle, /* [in] Handle of memory object */
1900 DWORD size, /* [in] New size of block */
1901 UINT flags /* [in] How to reallocate object */
1903 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1907 /***********************************************************************
1908 * LocalShrink (KERNEL32.@)
1910 UINT WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1912 return 0; /* LocalShrink does nothing in Win32 */
1916 /***********************************************************************
1917 * LocalSize (KERNEL32.@)
1922 UINT WINAPI LocalSize(
1923 HLOCAL handle /* [in] Handle of memory object */
1925 return GlobalSize( (HGLOBAL)handle );
1929 /***********************************************************************
1930 * LocalUnlock (KERNEL32.@)
1932 * TRUE: Object is still locked
1933 * FALSE: Object is unlocked
1935 BOOL WINAPI LocalUnlock(
1936 HLOCAL handle /* [in] Handle of memory object */
1938 return GlobalUnlock( (HGLOBAL)handle );