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"
25 #include "stackframe.h"
27 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(local);
31 DECLARE_DEBUG_CHANNEL(heap);
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) - 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 /* LocalNotify() msgs */
57 /* Layout of a handle entry table
59 * WORD count of entries
60 * LOCALHANDLEENTRY[count] entries
61 * WORD near ptr to next table
65 WORD addr; /* Address of the MOVEABLE block */
66 BYTE flags; /* Flags for this block */
67 BYTE lock; /* Lock count */
71 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
72 * in case something actually relies on this.
73 * Note that if the architecture does not allow unaligned accesses, we make
74 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
76 * An unused handle has lock = flags = 0xff. In windows addr is that of next
77 * free handle, at the moment in wine we set it to 0.
79 * A discarded block's handle has lock = addr = 0 and flags = 0x40
80 * (LMEM_DISCARDED >> 8)
83 #ifdef ALLOW_UNALIGNED_ACCESS
84 #define MOVEABLE_PREFIX sizeof(HLOCAL16)
86 #define MOVEABLE_PREFIX sizeof(int)
94 WORD check; /* 00 Heap checking flag */
95 WORD freeze; /* 02 Heap frozen flag */
96 WORD items; /* 04 Count of items on the heap */
97 WORD first; /* 06 First item of the heap */
98 WORD pad1; /* 08 Always 0 */
99 WORD last; /* 0a Last item of the heap */
100 WORD pad2; /* 0c Always 0 */
101 BYTE ncompact; /* 0e Compactions counter */
102 BYTE dislevel; /* 0f Discard level */
103 DWORD distotal; /* 10 Total bytes discarded */
104 WORD htable; /* 14 Pointer to handle table */
105 WORD hfree; /* 16 Pointer to free handle table */
106 WORD hdelta; /* 18 Delta to expand the handle table */
107 WORD expand; /* 1a Pointer to expand function (unused) */
108 WORD pstat; /* 1c Pointer to status structure (unused) */
109 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
110 WORD lock; /* 22 Lock count for the heap */
111 WORD extra; /* 24 Extra bytes to allocate when expanding */
112 WORD minsize; /* 26 Minimum size of the heap */
113 WORD magic; /* 28 Magic number */
118 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
120 WORD USER_HeapSel = 0; /* USER heap selector */
122 /* All local heap allocations are aligned on 4-byte boundaries */
123 #define LALIGN(word) (((word) + 3) & ~3)
125 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
126 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
127 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
128 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
130 /* determine whether the handle belongs to a fixed or a moveable block */
131 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
132 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
135 /* ### start build ### */
136 extern WORD CALLBACK LOCAL_CallTo16_word_www(FARPROC16,WORD,HLOCAL16,WORD);
137 /* ### stop build ### */
139 /***********************************************************************
142 * Return a pointer to the local heap, making sure it exists.
144 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
146 LOCALHEAPINFO *pInfo;
147 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
148 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
149 if (!ptr || !ptr->heap) return NULL;
150 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
152 WARN("Bad pointer\n");
155 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
156 if (pInfo->magic != LOCAL_HEAP_MAGIC)
165 /***********************************************************************
166 * LOCAL_MakeBlockFree
168 * Make a block free, inserting it in the free-list.
169 * 'block' is the handle of the block arena; 'baseptr' points to
170 * the beginning of the data segment containing the heap.
172 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
174 LOCALARENA *pArena, *pNext;
177 /* Mark the block as free */
179 pArena = ARENA_PTR( baseptr, block );
180 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
181 pArena->size = pArena->next - block;
183 /* Find the next free block (last block is always free) */
188 pNext = ARENA_PTR( baseptr, next );
189 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
193 TRACE("%04x, next %04x\n", block, next );
194 /* Insert the free block in the free-list */
196 pArena->free_prev = pNext->free_prev;
197 pArena->free_next = next;
198 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
199 pNext->free_prev = block;
203 /***********************************************************************
204 * LOCAL_RemoveFreeBlock
206 * Remove a block from the free-list.
207 * 'block' is the handle of the block arena; 'baseptr' points to
208 * the beginning of the data segment containing the heap.
210 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
212 /* Mark the block as fixed */
214 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
215 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
217 /* Remove it from the list */
219 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
220 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
224 /***********************************************************************
227 * Insert a new block in the heap.
228 * 'new' is the handle of the new block arena; 'baseptr' points to
229 * the beginning of the data segment containing the heap; 'prev' is
230 * the block before the new one.
232 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
234 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
235 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
237 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
238 pNew->next = pPrev->next;
239 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
240 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
245 /***********************************************************************
248 * Remove a block from the heap.
249 * 'block' is the handle of the block arena; 'baseptr' points to
250 * the beginning of the data segment containing the heap.
252 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
254 LOCALARENA *pArena, *pTmp;
256 /* Remove the block from the free-list */
259 pArena = ARENA_PTR( baseptr, block );
260 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
261 LOCAL_RemoveFreeBlock( baseptr, block );
263 /* If the previous block is free, expand its size */
265 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
266 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
267 pTmp->size += pArena->next - block;
269 /* Remove the block from the linked list */
271 pTmp->next = pArena->next;
272 pTmp = ARENA_PTR( baseptr, pArena->next );
273 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
277 /***********************************************************************
280 static void LOCAL_PrintHeap( HANDLE16 ds )
283 LOCALHEAPINFO *pInfo;
286 /* FIXME - the test should be done when calling the function!
287 plus is not clear that we should print this info
288 only when TRACE_ON is on! */
289 if(!TRACE_ON(local)) return;
291 ptr = MapSL( MAKESEGPTR( ds, 0 ));
292 pInfo = LOCAL_GetHeap( ds );
296 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
299 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
300 ds, pInfo->first, pInfo->last, pInfo->items );
302 arena = pInfo->first;
305 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
306 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
307 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
308 if (arena == pInfo->first)
310 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
311 pArena->size, pArena->free_prev, pArena->free_next );
313 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
315 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
316 pArena->size, pArena->free_prev, pArena->free_next );
317 if (pArena->next == arena) break; /* last one */
318 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
320 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
324 if (pArena->next == arena)
326 DPRINTF( "*** last block is not marked free\n" );
329 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
331 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
332 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
335 arena = pArena->next;
340 /***********************************************************************
341 * LocalInit (KERNEL.4)
343 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
346 WORD heapInfoArena, freeArena, lastArena;
347 LOCALHEAPINFO *pHeapInfo;
348 LOCALARENA *pArena, *pFirstArena, *pLastArena;
352 /* The initial layout of the heap is: */
353 /* - first arena (FIXED) */
354 /* - heap info structure (FIXED) */
355 /* - large free block (FREE) */
356 /* - last arena (FREE) */
358 TRACE("%04x %04x-%04x\n", selector, start, end);
359 if (!selector) selector = CURRENT_DS;
363 /* If TRACE_ON(heap) is set, the global heap blocks are */
364 /* cleared before use, so we can test for double initialization. */
365 if (LOCAL_GetHeap(selector))
367 ERR("Heap %04x initialized twice.\n", selector);
368 LOCAL_PrintHeap(selector);
374 /* start == 0 means: put the local heap at the end of the segment */
376 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
377 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
378 if ( end > 0xfffe ) end = 0xfffe;
384 if ((pModule = NE_GetPtr( GlobalHandle16( selector ) )))
386 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
389 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
390 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
393 if ( segNr < pModule->seg_count )
395 WORD minsize = pSeg->minsize;
396 if ( pModule->ss == segNr+1 )
397 minsize += pModule->stack_size;
399 TRACE(" new start %04x, minstart: %04x\n", start, minsize);
403 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
405 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
406 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
407 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
408 + sizeof(LOCALHEAPINFO) );
409 lastArena = (end - sizeof(LOCALARENA)) & ~3;
411 /* Make sure there's enough space. */
413 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
415 /* Initialise the first arena */
417 pFirstArena = ARENA_PTR( ptr, start );
418 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
419 pFirstArena->next = heapInfoArena;
420 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
421 pFirstArena->free_prev = start; /* this one */
422 pFirstArena->free_next = freeArena;
424 /* Initialise the arena of the heap info structure */
426 pArena = ARENA_PTR( ptr, heapInfoArena );
427 pArena->prev = start | LOCAL_ARENA_FIXED;
428 pArena->next = freeArena;
430 /* Initialise the heap info structure */
432 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
433 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
434 pHeapInfo->items = 4;
435 pHeapInfo->first = start;
436 pHeapInfo->last = lastArena;
437 pHeapInfo->htable = 0;
438 pHeapInfo->hdelta = 0x20;
439 pHeapInfo->extra = 0x200;
440 pHeapInfo->minsize = lastArena - freeArena;
441 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
443 /* Initialise the large free block */
445 pArena = ARENA_PTR( ptr, freeArena );
446 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
447 pArena->next = lastArena;
448 pArena->size = lastArena - freeArena;
449 pArena->free_prev = start;
450 pArena->free_next = lastArena;
452 /* Initialise the last block */
454 pLastArena = ARENA_PTR( ptr, lastArena );
455 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
456 pLastArena->next = lastArena; /* this one */
457 pLastArena->size = LALIGN(sizeof(LOCALARENA));
458 pLastArena->free_prev = freeArena;
459 pLastArena->free_next = lastArena; /* this one */
461 /* Store the local heap address in the instance data */
463 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
464 LOCAL_PrintHeap( selector );
468 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
472 /***********************************************************************
475 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
480 LOCALHEAPINFO *pHeapInfo;
481 WORD freeArena, lastArena;
482 LOCALARENA *pArena, *pLastArena;
485 hseg = GlobalHandle16( ds );
486 /* maybe mem allocated by Virtual*() ? */
487 if (!hseg) return FALSE;
489 oldsize = GlobalSize16( hseg );
490 /* if nothing can be gained, return */
491 if (oldsize > 0xfff0) return FALSE;
492 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
493 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
494 pHeapInfo = LOCAL_GetHeap( ds );
495 if (pHeapInfo == NULL) {
496 ERR("Heap not found\n" );
499 end = GlobalSize16( hseg );
500 lastArena = (end - sizeof(LOCALARENA)) & ~3;
502 /* Update the HeapInfo */
504 freeArena = pHeapInfo->last;
505 pHeapInfo->last = lastArena;
506 pHeapInfo->minsize += end - oldsize;
508 /* grow the old last block */
509 pArena = ARENA_PTR( ptr, freeArena );
510 pArena->size = lastArena - freeArena;
511 pArena->next = lastArena;
512 pArena->free_next = lastArena;
514 /* Initialise the new last block */
516 pLastArena = ARENA_PTR( ptr, lastArena );
517 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
518 pLastArena->next = lastArena; /* this one */
519 pLastArena->size = LALIGN(sizeof(LOCALARENA));
520 pLastArena->free_prev = freeArena;
521 pLastArena->free_next = lastArena; /* this one */
523 /* If block before freeArena is also free then merge them */
524 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
526 LOCAL_RemoveBlock(ptr, freeArena);
530 TRACE("Heap expanded\n" );
531 LOCAL_PrintHeap( ds );
536 /***********************************************************************
539 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
541 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
542 LOCALHEAPINFO *pInfo;
543 LOCALARENA *pArena, *pPrev, *pNext;
545 TRACE("%04x ds=%04x\n", arena, ds );
546 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
548 pArena = ARENA_PTR( ptr, arena );
549 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
551 /* shouldn't happen */
552 ERR("Trying to free block %04x twice!\n",
554 LOCAL_PrintHeap( ds );
558 /* Check if we can merge with the previous block */
560 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
561 pNext = ARENA_PTR( ptr, pArena->next );
562 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
564 arena = pArena->prev & ~3;
566 LOCAL_RemoveBlock( ptr, pPrev->next );
569 else /* Make a new free block */
571 LOCAL_MakeBlockFree( ptr, arena );
574 /* Check if we can merge with the next block */
576 if ((pArena->next == pArena->free_next) &&
577 (pArena->next != pInfo->last))
579 LOCAL_RemoveBlock( ptr, pArena->next );
586 /***********************************************************************
589 * Shrink an arena by creating a free block at its end if possible.
590 * 'size' includes the arena header, and must be aligned.
592 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
594 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
595 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
597 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
599 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
601 LOCAL_AddBlock( ptr, arena, arena + size );
603 LOCAL_FreeArena( ds, arena + size );
608 /***********************************************************************
609 * LOCAL_GrowArenaDownward
611 * Grow an arena downward by using the previous arena (must be free).
613 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
615 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
616 LOCALHEAPINFO *pInfo;
617 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
618 WORD prevArena = pArena->prev & ~3;
619 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
623 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
624 offset = pPrevArena->size;
625 size = pArena->next - arena - ARENA_HEADER_SIZE;
626 LOCAL_RemoveFreeBlock( ptr, prevArena );
627 LOCAL_RemoveBlock( ptr, arena );
629 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
630 while (offset < size)
632 memcpy( p, p + offset, offset );
636 if (size) memcpy( p, p + offset, size );
637 LOCAL_ShrinkArena( ds, prevArena, newsize );
642 /***********************************************************************
643 * LOCAL_GrowArenaUpward
645 * Grow an arena upward by using the next arena (must be free and big
646 * enough). Newsize includes the arena header and must be aligned.
648 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
650 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
651 LOCALHEAPINFO *pInfo;
652 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
653 WORD nextArena = pArena->next;
655 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
656 LOCAL_RemoveBlock( ptr, nextArena );
658 LOCAL_ShrinkArena( ds, arena, newsize );
662 /***********************************************************************
665 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
667 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
668 LOCALHEAPINFO *pInfo;
673 if (!(pInfo = LOCAL_GetHeap( ds )))
675 ERR("Local heap not found\n" );
679 arena = pInfo->first;
680 pArena = ARENA_PTR( ptr, arena );
681 while (arena != pArena->free_next)
683 arena = pArena->free_next;
684 pArena = ARENA_PTR( ptr, arena );
685 if (pArena->size >= freespace) freespace = pArena->size;
687 /* FIXME doesn't yet calculate space that would become free if everything
688 were discarded when countdiscard == 1 */
689 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
690 else freespace -= ARENA_HEADER_SIZE;
695 /***********************************************************************
698 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
700 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
701 LOCALHEAPINFO *pInfo;
702 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
703 WORD arena, movearena, finalarena, table;
704 WORD count, movesize, size;
706 LOCALHANDLEENTRY *pEntry;
708 if (!(pInfo = LOCAL_GetHeap( ds )))
710 ERR("Local heap not found\n" );
714 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
716 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
717 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
719 TRACE("Returning %04x.\n", freespace);
722 TRACE("Compacting heap %04x.\n", ds);
723 table = pInfo->htable;
726 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
727 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
729 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
731 /* OK we can move this one if we want */
732 TRACE("handle %04x (block %04x) can be moved.\n",
733 (WORD)((char *)pEntry - ptr), pEntry->addr);
734 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
735 pMoveArena = ARENA_PTR(ptr, movearena);
736 movesize = pMoveArena->next - movearena;
737 arena = pInfo->first;
738 pArena = ARENA_PTR(ptr, arena);
741 /* Try to find the smallest arena that will do, */
742 /* which is below us in memory */
745 arena = pArena->free_next;
746 pArena = ARENA_PTR(ptr, arena);
747 if(arena >= movearena)
749 if(arena == pArena->free_next)
751 if((pArena->size >= movesize) && (pArena->size < size))
757 if (finalarena) /* Actually got somewhere to move */
759 TRACE("Moving it to %04x.\n", finalarena);
760 pFinalArena = ARENA_PTR(ptr, finalarena);
761 size = pFinalArena->size;
762 LOCAL_RemoveFreeBlock(ptr, finalarena);
763 LOCAL_ShrinkArena( ds, finalarena, movesize );
764 /* Copy the arena to it's new location */
765 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
766 (char *)pMoveArena + ARENA_HEADER_SIZE,
767 movesize - ARENA_HEADER_SIZE );
768 /* Free the old location */
769 LOCAL_FreeArena(ds, movearena);
771 LOCAL_CallTo16_word_www(pInfo->notify, LN_MOVE,
772 (WORD)((char *)pEntry - ptr), pEntry->addr);
773 /* Update handle table entry */
774 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
776 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
779 /* Previous arena is free (but < movesize) */
780 /* so we can 'slide' movearena down into it */
781 finalarena = pMoveArena->prev & ~3;
782 LOCAL_GrowArenaDownward( ds, movearena, movesize );
783 /* Update handle table entry */
784 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
788 table = *(WORD *)pEntry;
790 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
791 if(freespace >= minfree || (flags & LMEM_NODISCARD))
793 TRACE("Returning %04x.\n", freespace);
797 table = pInfo->htable;
800 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
801 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
803 if(pEntry->addr && pEntry->lock == 0 &&
804 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
806 TRACE("Discarding handle %04x (block %04x).\n",
807 (char *)pEntry - ptr, pEntry->addr);
808 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
810 LOCAL_CallTo16_word_www(pInfo->notify, LN_DISCARD,
811 (char *)pEntry - ptr, pEntry->flags);
813 pEntry->flags = (LMEM_DISCARDED >> 8);
816 table = *(WORD *)pEntry;
818 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
822 /***********************************************************************
823 * LOCAL_FindFreeBlock
825 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
827 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
828 LOCALHEAPINFO *pInfo;
832 if (!(pInfo = LOCAL_GetHeap( ds )))
834 ERR("Local heap not found\n" );
839 arena = pInfo->first;
840 pArena = ARENA_PTR( ptr, arena );
842 arena = pArena->free_next;
843 pArena = ARENA_PTR( ptr, arena );
844 if (arena == pArena->free_next) break;
845 if (pArena->size >= size) return arena;
847 TRACE("not enough space\n" );
853 /***********************************************************************
856 static const char *get_heap_name( WORD ds )
858 HINSTANCE16 inst = LoadLibrary16( "GDI" );
859 if (ds == GlobalHandleToSel16( inst ))
861 FreeLibrary16( inst );
864 FreeLibrary16( inst );
865 inst = LoadLibrary16( "USER" );
866 if (ds == GlobalHandleToSel16( inst ))
868 FreeLibrary16( inst );
871 FreeLibrary16( inst );
875 /***********************************************************************
877 * The segment may get moved around in this function, so all callers
878 * should reset their pointer variables.
880 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
882 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
883 LOCALHEAPINFO *pInfo;
887 if (!(pInfo = LOCAL_GetHeap( ds )))
889 ERR("Local heap not found\n");
894 size += ARENA_HEADER_SIZE;
895 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
900 /* Find a suitable free block */
901 arena = LOCAL_FindFreeBlock( ds, size );
903 /* no space: try to make some */
904 LOCAL_Compact( ds, size, flags );
905 arena = LOCAL_FindFreeBlock( ds, size );
908 /* still no space: try to grow the segment */
909 if (!(LOCAL_GrowHeap( ds )))
912 /* FIXME: doesn't work correctly yet */
913 if ((pInfo->notify) && (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds - 20, size))) /* FIXME: "size" correct ? (should indicate bytes needed) */
918 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
919 pInfo = LOCAL_GetHeap( ds );
920 arena = LOCAL_FindFreeBlock( ds, size );
923 ERR( "not enough space in %s heap %04x for %d bytes\n",
924 get_heap_name(ds), ds, size );
926 if ((pInfo->notify) &&
927 /* FIXME: "size" correct ? (should indicate bytes needed) */
928 (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds, size)))
934 /* Make a block out of the free arena */
935 pArena = ARENA_PTR( ptr, arena );
936 TRACE("size = %04x, arena %04x size %04x\n",
937 size, arena, pArena->size );
938 LOCAL_RemoveFreeBlock( ptr, arena );
939 LOCAL_ShrinkArena( ds, arena, size );
941 if (flags & LMEM_ZEROINIT)
942 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
943 return arena + ARENA_HEADER_SIZE;
947 /***********************************************************************
950 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
952 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
953 LOCALHEAPINFO *pInfo;
954 LOCALHANDLEENTRY *pEntry;
959 if (!(pInfo = LOCAL_GetHeap( ds )))
961 ERR("Local heap not found\n");
966 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
967 + 2 * sizeof(WORD), LMEM_FIXED )))
969 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
970 ERR("ptr == NULL after GetBlock.\n");
971 if (!(pInfo = LOCAL_GetHeap( ds )))
972 ERR("pInfo == NULL after GetBlock.\n");
974 /* Fill the entry table */
976 *(WORD *)(ptr + handle) = pInfo->hdelta;
977 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
978 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
979 pEntry->lock = pEntry->flags = 0xff;
982 *(WORD *)pEntry = pInfo->htable;
983 pInfo->htable = handle;
988 /***********************************************************************
989 * LOCAL_GetNewHandleEntry
991 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
993 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
994 LOCALHEAPINFO *pInfo;
995 LOCALHANDLEENTRY *pEntry = NULL;
998 if (!(pInfo = LOCAL_GetHeap( ds )))
1000 ERR("Local heap not found\n");
1001 LOCAL_PrintHeap(ds);
1005 /* Find a free slot in existing tables */
1007 table = pInfo->htable;
1010 WORD count = *(WORD *)(ptr + table);
1011 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1012 for (; count > 0; count--, pEntry++)
1013 if (pEntry->lock == 0xff) break;
1015 table = *(WORD *)pEntry;
1018 if (!table) /* We need to create a new table */
1020 if (!LOCAL_NewHTable( ds )) return 0;
1021 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1022 pInfo = LOCAL_GetHeap( ds );
1023 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1026 /* Now allocate this entry */
1030 TRACE("(%04x): %04x\n",
1031 ds, ((char *)pEntry - ptr) );
1032 return (HLOCAL16)((char *)pEntry - ptr);
1036 /***********************************************************************
1037 * LOCAL_FreeHandleEntry
1039 * Free a handle table entry.
1041 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1043 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1044 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1045 LOCALHEAPINFO *pInfo;
1047 WORD table, count, i;
1049 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1051 /* Find the table where this handle comes from */
1053 pTable = &pInfo->htable;
1056 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1057 if ((handle >= *pTable + sizeof(WORD)) &&
1058 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1059 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1063 ERR("Invalid entry %04x\n", handle);
1064 LOCAL_PrintHeap( ds );
1068 /* Make the entry free */
1070 pEntry->addr = 0; /* just in case */
1071 pEntry->lock = 0xff;
1072 pEntry->flags = 0xff;
1073 /* Now check if all entries in this table are free */
1076 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1077 count = *(WORD *)(ptr + table);
1078 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1080 /* Remove the table from the linked list and free it */
1082 TRACE("(%04x): freeing table %04x\n",
1084 *pTable = *(WORD *)pEntry;
1085 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1089 /***********************************************************************
1092 * Implementation of LocalFree().
1094 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1096 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1098 TRACE("%04x ds=%04x\n", handle, ds );
1100 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1101 if (HANDLE_FIXED( handle ))
1103 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1104 else return handle; /* couldn't free it */
1108 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1109 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1111 TRACE("real block at %04x\n",
1113 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1114 return handle; /* couldn't free it */
1116 LOCAL_FreeHandleEntry( ds, handle );
1122 /***********************************************************************
1125 * Implementation of LocalAlloc().
1128 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1133 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1135 if(size > 0 && size <= 4) size = 5;
1136 if (flags & LMEM_MOVEABLE)
1138 LOCALHANDLEENTRY *plhe;
1143 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1146 else /* We just need to allocate a discarded handle */
1148 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1150 WARN("Couldn't get handle.\n");
1152 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1155 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1156 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1160 plhe->addr = hmem + MOVEABLE_PREFIX;
1161 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1162 *(HLOCAL16 *)(ptr + hmem) = handle;
1167 plhe->flags = LMEM_DISCARDED >> 8;
1174 handle = LOCAL_GetBlock( ds, size, flags );
1180 /***********************************************************************
1183 * Implementation of LocalReAlloc().
1185 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1187 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1188 LOCALHEAPINFO *pInfo;
1189 LOCALARENA *pArena, *pNext;
1190 LOCALHANDLEENTRY *pEntry = NULL;
1191 WORD arena, oldsize;
1192 HLOCAL16 hmem, blockhandle;
1195 if (!handle) return 0;
1196 if(HANDLE_MOVEABLE(handle) &&
1197 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1200 TRACE("%04x %d %04x ds=%04x\n",
1201 handle, size, flags, ds );
1202 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1204 if (HANDLE_FIXED( handle ))
1205 blockhandle = handle;
1208 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1209 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1213 WARN("Dicarded block has non-zero addr.\n");
1214 TRACE("ReAllocating discarded block\n");
1215 if(size <= 4) size = 5;
1216 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1218 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1219 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1220 pEntry->addr = hl + MOVEABLE_PREFIX;
1223 *(HLOCAL16 *)(ptr + hl) = handle;
1226 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1228 ERR("(%04x,%04x): invalid handle\n",
1232 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1233 ERR("Back ptr to handle is invalid\n");
1238 if (flags & LMEM_MODIFY)
1240 if (HANDLE_MOVEABLE(handle))
1242 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1243 pEntry->flags = (flags & 0x0f00) >> 8;
1244 TRACE("Changing flags to %x.\n", pEntry->flags);
1251 if (flags & LMEM_MOVEABLE)
1253 if (HANDLE_FIXED(handle))
1255 TRACE("Freeing fixed block.\n");
1256 return LOCAL_Free( ds, handle );
1258 else /* Moveable block */
1260 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1261 if (pEntry->lock == 0)
1263 /* discards moveable blocks */
1264 TRACE("Discarding block\n");
1265 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1267 pEntry->flags = (LMEM_DISCARDED >> 8);
1275 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1276 if (pEntry->lock == 0)
1279 return LOCAL_Free( ds, handle );
1285 arena = ARENA_HEADER( blockhandle );
1286 TRACE("arena is %04x\n", arena );
1287 pArena = ARENA_PTR( ptr, arena );
1289 if(size <= 4) size = 5;
1290 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1291 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1292 nextarena = LALIGN(blockhandle + size);
1294 /* Check for size reduction */
1296 if (nextarena <= pArena->next)
1298 TRACE("size reduction, making new free block\n");
1299 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1300 TRACE("returning %04x\n", handle );
1304 /* Check if the next block is free and large enough */
1306 pNext = ARENA_PTR( ptr, pArena->next );
1307 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1308 (nextarena <= pNext->next))
1310 TRACE("size increase, making new free block\n");
1311 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1312 TRACE("returning %04x\n", handle );
1316 /* Now we have to allocate a new block, but not if (fixed block or locked
1317 block) and no LMEM_MOVEABLE */
1319 if (!(flags & LMEM_MOVEABLE))
1321 if (HANDLE_FIXED(handle))
1323 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1328 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1330 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1336 hmem = LOCAL_GetBlock( ds, size, flags );
1337 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1338 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1339 { /* a compaction, which might in turn have */
1340 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1341 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1345 /* Remove the block from the heap and try again */
1346 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1347 if (!buffer) return 0;
1348 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1349 LOCAL_FreeArena( ds, arena );
1350 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1352 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1354 ERR("Can't restore saved block\n" );
1355 HeapFree( GetProcessHeap(), 0, buffer );
1360 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1361 memcpy( ptr + hmem, buffer, oldsize );
1362 HeapFree( GetProcessHeap(), 0, buffer );
1366 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1367 LOCAL_FreeArena( ds, arena );
1369 if (HANDLE_MOVEABLE( handle ))
1371 TRACE("fixing handle\n");
1372 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1373 pEntry->addr = hmem + MOVEABLE_PREFIX;
1374 /* Back ptr should still be correct */
1375 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1376 ERR("back ptr is invalid.\n");
1379 if (size == oldsize) hmem = 0; /* Realloc failed */
1380 TRACE("returning %04x\n", hmem );
1385 /***********************************************************************
1386 * LOCAL_InternalLock
1388 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1390 HLOCAL16 old_handle = handle;
1392 if (HANDLE_MOVEABLE(handle))
1394 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1395 if (pEntry->flags == LMEM_DISCARDED) return 0;
1396 if (pEntry->lock < 0xfe) pEntry->lock++;
1397 handle = pEntry->addr;
1399 TRACE("%04x returning %04x\n",
1400 old_handle, handle );
1405 /***********************************************************************
1408 void *LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1410 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1411 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1415 /***********************************************************************
1418 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1420 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1422 TRACE("%04x\n", handle );
1423 if (HANDLE_MOVEABLE(handle))
1425 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1426 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1427 /* For moveable block, return the new lock count */
1428 /* (see _Windows_Internals_ p. 197) */
1429 return --pEntry->lock;
1435 /***********************************************************************
1438 * Implementation of LocalSize().
1440 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1442 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1445 TRACE("%04x ds=%04x\n", handle, ds );
1447 if (!handle) return 0;
1448 if (HANDLE_MOVEABLE( handle ))
1450 handle = *(WORD *)(ptr + handle);
1451 if (!handle) return 0;
1452 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1455 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1457 return pArena->next - handle;
1461 /***********************************************************************
1464 * Implementation of LocalFlags().
1466 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1468 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1470 if (HANDLE_MOVEABLE(handle))
1472 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1473 TRACE("(%04x,%04x): returning %04x\n",
1474 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1475 return pEntry->lock | (pEntry->flags << 8);
1479 TRACE("(%04x,%04x): returning 0\n",
1486 /***********************************************************************
1489 * Implementation of LocalHeapSize().
1491 WORD LOCAL_HeapSize( HANDLE16 ds )
1493 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1494 if (!pInfo) return 0;
1495 return pInfo->last - pInfo->first;
1499 /***********************************************************************
1502 * Implementation of LocalCountFree().
1504 WORD LOCAL_CountFree( HANDLE16 ds )
1508 LOCALHEAPINFO *pInfo;
1509 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1511 if (!(pInfo = LOCAL_GetHeap( ds )))
1513 ERR("(%04x): Local heap not found\n", ds );
1514 LOCAL_PrintHeap( ds );
1519 arena = pInfo->first;
1520 pArena = ARENA_PTR( ptr, arena );
1523 arena = pArena->free_next;
1524 pArena = ARENA_PTR( ptr, arena );
1525 if (arena == pArena->free_next) break;
1526 total += pArena->size;
1528 TRACE("(%04x): returning %d\n", ds, total);
1533 /***********************************************************************
1536 * Implementation of LocalHandle().
1538 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1540 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1541 LOCALHEAPINFO *pInfo;
1544 if (!(pInfo = LOCAL_GetHeap( ds )))
1546 ERR("(%04x): Local heap not found\n", ds );
1547 LOCAL_PrintHeap( ds );
1551 /* Find the address in the entry tables */
1553 table = pInfo->htable;
1556 WORD count = *(WORD *)(ptr + table);
1557 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1558 for (; count > 0; count--, pEntry++)
1559 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1560 table = *(WORD *)pEntry;
1563 return (HLOCAL16)addr; /* Fixed block handle is addr */
1567 /***********************************************************************
1568 * LocalAlloc16 (KERNEL.5)
1570 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1572 HLOCAL16 ret = LOCAL_Alloc( CURRENT_DS, flags, size );
1573 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
1578 /***********************************************************************
1579 * LocalReAlloc16 (KERNEL.6)
1581 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1583 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1587 /***********************************************************************
1588 * LocalFree16 (KERNEL.7)
1590 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1592 return LOCAL_Free( CURRENT_DS, handle );
1596 /***********************************************************************
1597 * LocalLock16 (KERNEL.8)
1599 * Note: only the offset part of the pointer is returned by the relay code.
1601 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1603 WORD ds = CURRENT_DS;
1604 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1605 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1609 /***********************************************************************
1610 * LocalUnlock16 (KERNEL.9)
1612 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1614 return LOCAL_Unlock( CURRENT_DS, handle );
1618 /***********************************************************************
1619 * LocalSize16 (KERNEL.10)
1621 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1623 return LOCAL_Size( CURRENT_DS, handle );
1627 /***********************************************************************
1628 * LocalHandle16 (KERNEL.11)
1630 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1632 return LOCAL_Handle( CURRENT_DS, addr );
1636 /***********************************************************************
1637 * LocalFlags16 (KERNEL.12)
1639 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1641 return LOCAL_Flags( CURRENT_DS, handle );
1645 /***********************************************************************
1646 * LocalCompact16 (KERNEL.13)
1648 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1650 TRACE("%04x\n", minfree );
1651 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1655 /***********************************************************************
1656 * LocalNotify (KERNEL.14)
1658 * Installs a callback function that is called for local memory events
1659 * Callback function prototype is
1660 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1663 * NotifyFunc seems to be responsible for allocating some memory,
1664 * returns TRUE for success.
1665 * wArg = number of bytes needed additionally
1667 * hMem = handle; wArg = old mem location
1669 * NotifyFunc seems to be strongly encouraged to return TRUE,
1670 * otherwise LogError() gets called.
1671 * hMem = handle; wArg = flags
1673 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1675 LOCALHEAPINFO *pInfo;
1676 FARPROC16 oldNotify;
1677 HANDLE16 ds = CURRENT_DS;
1679 if (!(pInfo = LOCAL_GetHeap( ds )))
1681 ERR("(%04x): Local heap not found\n", ds );
1682 LOCAL_PrintHeap( ds );
1685 TRACE("(%04x): %08lx\n", ds, (DWORD)func );
1686 FIXME("Half implemented\n");
1687 oldNotify = pInfo->notify;
1688 pInfo->notify = func;
1693 /***********************************************************************
1694 * LocalShrink16 (KERNEL.121)
1696 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1698 TRACE("%04x %04x\n", handle, newsize );
1703 /***********************************************************************
1704 * GetHeapSpaces (KERNEL.138)
1706 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1711 if (!(pModule = NE_GetPtr( module ))) return 0;
1713 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1714 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1718 /***********************************************************************
1719 * LocalCountFree (KERNEL.161)
1721 WORD WINAPI LocalCountFree16(void)
1723 return LOCAL_CountFree( CURRENT_DS );
1727 /***********************************************************************
1728 * LocalHeapSize (KERNEL.162)
1730 WORD WINAPI LocalHeapSize16(void)
1733 return LOCAL_HeapSize( CURRENT_DS );
1737 /***********************************************************************
1738 * LocalHandleDelta (KERNEL.310)
1740 WORD WINAPI LocalHandleDelta16( WORD delta )
1742 LOCALHEAPINFO *pInfo;
1744 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1746 ERR("Local heap not found\n");
1747 LOCAL_PrintHeap( CURRENT_DS );
1750 if (delta) pInfo->hdelta = delta;
1751 TRACE("returning %04x\n", pInfo->hdelta);
1752 return pInfo->hdelta;
1756 /***********************************************************************
1757 * LocalInfo (TOOLHELP.56)
1759 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1761 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle)));
1762 if (!pInfo) return FALSE;
1763 pLocalInfo->wcItems = pInfo->items;
1768 /***********************************************************************
1769 * LocalFirst (TOOLHELP.57)
1771 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1773 WORD ds = GlobalHandleToSel16( handle );
1774 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1775 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1776 if (!pInfo) return FALSE;
1778 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1779 pLocalEntry->wAddress = pLocalEntry->hHandle;
1780 pLocalEntry->wFlags = LF_FIXED;
1781 pLocalEntry->wcLock = 0;
1782 pLocalEntry->wType = LT_NORMAL;
1783 pLocalEntry->hHeap = handle;
1784 pLocalEntry->wHeapType = NORMAL_HEAP;
1785 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1786 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1791 /***********************************************************************
1792 * LocalNext (TOOLHELP.58)
1794 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1796 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1797 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1800 if (!LOCAL_GetHeap( ds )) return FALSE;
1801 if (!pLocalEntry->wNext) return FALSE;
1802 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1804 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1805 pLocalEntry->wAddress = pLocalEntry->hHandle;
1806 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1807 pLocalEntry->wcLock = 0;
1808 pLocalEntry->wType = LT_NORMAL;
1809 if (pArena->next != pLocalEntry->wNext) /* last one? */
1810 pLocalEntry->wNext = pArena->next;
1812 pLocalEntry->wNext = 0;
1813 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1818 /***********************************************************************
1819 * LocalAlloc (KERNEL32.371)
1824 HLOCAL WINAPI LocalAlloc(
1825 UINT flags, /* [in] Allocation attributes */
1826 DWORD size /* [in] Number of bytes to allocate */
1828 return (HLOCAL)GlobalAlloc( flags, size );
1832 /***********************************************************************
1833 * LocalCompact (KERNEL32.372)
1835 UINT WINAPI LocalCompact( UINT minfree )
1837 return 0; /* LocalCompact does nothing in Win32 */
1841 /***********************************************************************
1842 * LocalFlags (KERNEL32.374)
1844 * Value specifying allocation flags and lock count.
1845 * LMEM_INVALID_HANDLE: Failure
1847 UINT WINAPI LocalFlags(
1848 HLOCAL handle /* [in] Handle of memory object */
1850 return GlobalFlags( (HGLOBAL)handle );
1854 /***********************************************************************
1855 * LocalFree (KERNEL32.375)
1860 HLOCAL WINAPI LocalFree(
1861 HLOCAL handle /* [in] Handle of memory object */
1863 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
1867 /***********************************************************************
1868 * LocalHandle (KERNEL32.376)
1873 HLOCAL WINAPI LocalHandle(
1874 LPCVOID ptr /* [in] Address of local memory object */
1876 return (HLOCAL)GlobalHandle( ptr );
1880 /***********************************************************************
1881 * LocalLock (KERNEL32.377)
1882 * Locks a local memory object and returns pointer to the first byte
1883 * of the memory block.
1889 LPVOID WINAPI LocalLock(
1890 HLOCAL handle /* [in] Address of local memory object */
1892 return GlobalLock( (HGLOBAL)handle );
1896 /***********************************************************************
1897 * LocalReAlloc (KERNEL32.378)
1902 HLOCAL WINAPI LocalReAlloc(
1903 HLOCAL handle, /* [in] Handle of memory object */
1904 DWORD size, /* [in] New size of block */
1905 UINT flags /* [in] How to reallocate object */
1907 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1911 /***********************************************************************
1912 * LocalShrink (KERNEL32.379)
1914 UINT WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1916 return 0; /* LocalShrink does nothing in Win32 */
1920 /***********************************************************************
1921 * LocalSize (KERNEL32.380)
1926 UINT WINAPI LocalSize(
1927 HLOCAL handle /* [in] Handle of memory object */
1929 return GlobalSize( (HGLOBAL)handle );
1933 /***********************************************************************
1934 * LocalUnlock (KERNEL32.381)
1936 * TRUE: Object is still locked
1937 * FALSE: Object is unlocked
1939 BOOL WINAPI LocalUnlock(
1940 HLOCAL handle /* [in] Handle of memory object */
1942 return GlobalUnlock( (HGLOBAL)handle );