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"
24 #include "stackframe.h"
26 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(local
);
30 DECLARE_DEBUG_CHANNEL(heap
);
35 WORD prev
; /* Previous arena | arena type */
36 WORD next
; /* Next arena */
37 /* Start of the memory block or free-list info */
38 WORD size
; /* Size of the free block */
39 WORD free_prev
; /* Previous free block */
40 WORD free_next
; /* Next free block */
43 #define ARENA_HEADER_SIZE 4
44 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
46 /* Arena types (stored in 'prev' field of the arena) */
47 #define LOCAL_ARENA_FREE 0
48 #define LOCAL_ARENA_FIXED 1
50 /* LocalNotify() msgs */
56 /* Layout of a handle entry table
58 * WORD count of entries
59 * LOCALHANDLEENTRY[count] entries
60 * WORD near ptr to next table
64 WORD addr
; /* Address of the MOVEABLE block */
65 BYTE flags
; /* Flags for this block */
66 BYTE lock
; /* Lock count */
70 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
71 * in case something actually relies on this.
72 * Note that if the architecture does not allow unaligned accesses, we make
73 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
75 * An unused handle has lock = flags = 0xff. In windows addr is that of next
76 * free handle, at the moment in wine we set it to 0.
78 * A discarded block's handle has lock = addr = 0 and flags = 0x40
79 * (LMEM_DISCARDED >> 8)
82 #ifdef ALLOW_UNALIGNED_ACCESS
83 #define MOVEABLE_PREFIX sizeof(HLOCAL16)
85 #define MOVEABLE_PREFIX sizeof(int)
93 WORD check
; /* 00 Heap checking flag */
94 WORD freeze
; /* 02 Heap frozen flag */
95 WORD items
; /* 04 Count of items on the heap */
96 WORD first
; /* 06 First item of the heap */
97 WORD pad1
; /* 08 Always 0 */
98 WORD last
; /* 0a Last item of the heap */
99 WORD pad2
; /* 0c Always 0 */
100 BYTE ncompact
; /* 0e Compactions counter */
101 BYTE dislevel
; /* 0f Discard level */
102 DWORD distotal
; /* 10 Total bytes discarded */
103 WORD htable
; /* 14 Pointer to handle table */
104 WORD hfree
; /* 16 Pointer to free handle table */
105 WORD hdelta
; /* 18 Delta to expand the handle table */
106 WORD expand
; /* 1a Pointer to expand function (unused) */
107 WORD pstat
; /* 1c Pointer to status structure (unused) */
108 FARPROC16 notify WINE_PACKED
; /* 1e Pointer to LocalNotify() function */
109 WORD lock
; /* 22 Lock count for the heap */
110 WORD extra
; /* 24 Extra bytes to allocate when expanding */
111 WORD minsize
; /* 26 Minimum size of the heap */
112 WORD magic
; /* 28 Magic number */
117 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
119 /* All local heap allocations are aligned on 4-byte boundaries */
120 #define LALIGN(word) (((word) + 3) & ~3)
122 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
123 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
124 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
125 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
127 /* determine whether the handle belongs to a fixed or a moveable block */
128 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
129 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
132 /* ### start build ### */
133 extern WORD CALLBACK
LOCAL_CallTo16_word_www(FARPROC16
,WORD
,HLOCAL16
,WORD
);
134 /* ### stop build ### */
136 /***********************************************************************
139 * Return a pointer to the local heap, making sure it exists.
141 static LOCALHEAPINFO
*LOCAL_GetHeap( HANDLE16 ds
)
143 LOCALHEAPINFO
*pInfo
;
144 INSTANCEDATA
*ptr
= MapSL( MAKESEGPTR( ds
, 0 ));
145 TRACE("Heap at %p, %04x\n", ptr
, (ptr
!= NULL
? ptr
->heap
: 0xFFFF));
146 if (!ptr
|| !ptr
->heap
) return NULL
;
147 if (IsBadReadPtr16( (SEGPTR
)MAKELONG(ptr
->heap
,ds
), sizeof(LOCALHEAPINFO
)))
149 WARN("Bad pointer\n");
152 pInfo
= (LOCALHEAPINFO
*)((char*)ptr
+ ptr
->heap
);
153 if (pInfo
->magic
!= LOCAL_HEAP_MAGIC
)
162 /***********************************************************************
163 * LOCAL_MakeBlockFree
165 * Make a block free, inserting it in the free-list.
166 * 'block' is the handle of the block arena; 'baseptr' points to
167 * the beginning of the data segment containing the heap.
169 static void LOCAL_MakeBlockFree( char *baseptr
, WORD block
)
171 LOCALARENA
*pArena
, *pNext
;
174 /* Mark the block as free */
176 pArena
= ARENA_PTR( baseptr
, block
);
177 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FREE
;
178 pArena
->size
= pArena
->next
- block
;
180 /* Find the next free block (last block is always free) */
185 pNext
= ARENA_PTR( baseptr
, next
);
186 if ((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) break;
190 TRACE("%04x, next %04x\n", block
, next
);
191 /* Insert the free block in the free-list */
193 pArena
->free_prev
= pNext
->free_prev
;
194 pArena
->free_next
= next
;
195 ARENA_PTR(baseptr
,pNext
->free_prev
)->free_next
= block
;
196 pNext
->free_prev
= block
;
200 /***********************************************************************
201 * LOCAL_RemoveFreeBlock
203 * Remove a block from the free-list.
204 * 'block' is the handle of the block arena; 'baseptr' points to
205 * the beginning of the data segment containing the heap.
207 static void LOCAL_RemoveFreeBlock( char *baseptr
, WORD block
)
209 /* Mark the block as fixed */
211 LOCALARENA
*pArena
= ARENA_PTR( baseptr
, block
);
212 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FIXED
;
214 /* Remove it from the list */
216 ARENA_PTR(baseptr
,pArena
->free_prev
)->free_next
= pArena
->free_next
;
217 ARENA_PTR(baseptr
,pArena
->free_next
)->free_prev
= pArena
->free_prev
;
221 /***********************************************************************
224 * Insert a new block in the heap.
225 * 'new' is the handle of the new block arena; 'baseptr' points to
226 * the beginning of the data segment containing the heap; 'prev' is
227 * the block before the new one.
229 static void LOCAL_AddBlock( char *baseptr
, WORD prev
, WORD
new )
231 LOCALARENA
*pPrev
= ARENA_PTR( baseptr
, prev
);
232 LOCALARENA
*pNew
= ARENA_PTR( baseptr
, new );
234 pNew
->prev
= (prev
& ~3) | LOCAL_ARENA_FIXED
;
235 pNew
->next
= pPrev
->next
;
236 ARENA_PTR(baseptr
,pPrev
->next
)->prev
&= 3;
237 ARENA_PTR(baseptr
,pPrev
->next
)->prev
|= new;
242 /***********************************************************************
245 * Remove a block from the heap.
246 * 'block' is the handle of the block arena; 'baseptr' points to
247 * the beginning of the data segment containing the heap.
249 static void LOCAL_RemoveBlock( char *baseptr
, WORD block
)
251 LOCALARENA
*pArena
, *pTmp
;
253 /* Remove the block from the free-list */
256 pArena
= ARENA_PTR( baseptr
, block
);
257 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
258 LOCAL_RemoveFreeBlock( baseptr
, block
);
260 /* If the previous block is free, expand its size */
262 pTmp
= ARENA_PTR( baseptr
, pArena
->prev
& ~3 );
263 if ((pTmp
->prev
& 3) == LOCAL_ARENA_FREE
)
264 pTmp
->size
+= pArena
->next
- block
;
266 /* Remove the block from the linked list */
268 pTmp
->next
= pArena
->next
;
269 pTmp
= ARENA_PTR( baseptr
, pArena
->next
);
270 pTmp
->prev
= (pTmp
->prev
& 3) | (pArena
->prev
& ~3);
274 /***********************************************************************
277 static void LOCAL_PrintHeap( HANDLE16 ds
)
280 LOCALHEAPINFO
*pInfo
;
283 /* FIXME - the test should be done when calling the function!
284 plus is not clear that we should print this info
285 only when TRACE_ON is on! */
286 if(!TRACE_ON(local
)) return;
288 ptr
= MapSL( MAKESEGPTR( ds
, 0 ));
289 pInfo
= LOCAL_GetHeap( ds
);
293 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds
);
296 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
297 ds
, pInfo
->first
, pInfo
->last
, pInfo
->items
);
299 arena
= pInfo
->first
;
302 LOCALARENA
*pArena
= ARENA_PTR(ptr
,arena
);
303 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena
,
304 pArena
->prev
& ~3, pArena
->next
, pArena
->prev
& 3 );
305 if (arena
== pInfo
->first
)
307 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
308 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
310 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
312 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
313 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
314 if (pArena
->next
== arena
) break; /* last one */
315 if (ARENA_PTR(ptr
,pArena
->free_next
)->free_prev
!= arena
)
317 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
321 if (pArena
->next
== arena
)
323 DPRINTF( "*** last block is not marked free\n" );
326 if ((ARENA_PTR(ptr
,pArena
->next
)->prev
& ~3) != arena
)
328 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
329 pArena
->next
, ARENA_PTR(ptr
,pArena
->next
)->prev
);
332 arena
= pArena
->next
;
337 /***********************************************************************
338 * LocalInit (KERNEL.4)
340 BOOL16 WINAPI
LocalInit16( HANDLE16 selector
, WORD start
, WORD end
)
343 WORD heapInfoArena
, freeArena
, lastArena
;
344 LOCALHEAPINFO
*pHeapInfo
;
345 LOCALARENA
*pArena
, *pFirstArena
, *pLastArena
;
349 /* The initial layout of the heap is: */
350 /* - first arena (FIXED) */
351 /* - heap info structure (FIXED) */
352 /* - large free block (FREE) */
353 /* - last arena (FREE) */
355 TRACE("%04x %04x-%04x\n", selector
, start
, end
);
356 if (!selector
) selector
= CURRENT_DS
;
360 /* If TRACE_ON(heap) is set, the global heap blocks are */
361 /* cleared before use, so we can test for double initialization. */
362 if (LOCAL_GetHeap(selector
))
364 ERR("Heap %04x initialized twice.\n", selector
);
365 LOCAL_PrintHeap(selector
);
371 /* start == 0 means: put the local heap at the end of the segment */
373 DWORD size
= GlobalSize16( GlobalHandle16( selector
) );
374 start
= (WORD
)(size
> 0xffff ? 0xffff : size
) - 1;
375 if ( end
> 0xfffe ) end
= 0xfffe;
381 if ((pModule
= NE_GetPtr( GlobalHandle16( selector
) )))
383 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
);
386 for ( segNr
= 0; segNr
< pModule
->seg_count
; segNr
++, pSeg
++ )
387 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
390 if ( segNr
< pModule
->seg_count
)
392 WORD minsize
= pSeg
->minsize
;
393 if ( pModule
->ss
== segNr
+1 )
394 minsize
+= pModule
->stack_size
;
396 TRACE(" new start %04x, minstart: %04x\n", start
, minsize
);
400 ptr
= MapSL( MAKESEGPTR( selector
, 0 ) );
402 start
= LALIGN( max( start
, sizeof(INSTANCEDATA
) ) );
403 heapInfoArena
= LALIGN(start
+ sizeof(LOCALARENA
) );
404 freeArena
= LALIGN( heapInfoArena
+ ARENA_HEADER_SIZE
405 + sizeof(LOCALHEAPINFO
) );
406 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
408 /* Make sure there's enough space. */
410 if (freeArena
+ sizeof(LOCALARENA
) >= lastArena
) goto done
;
412 /* Initialise the first arena */
414 pFirstArena
= ARENA_PTR( ptr
, start
);
415 pFirstArena
->prev
= start
| LOCAL_ARENA_FIXED
;
416 pFirstArena
->next
= heapInfoArena
;
417 pFirstArena
->size
= LALIGN(sizeof(LOCALARENA
));
418 pFirstArena
->free_prev
= start
; /* this one */
419 pFirstArena
->free_next
= freeArena
;
421 /* Initialise the arena of the heap info structure */
423 pArena
= ARENA_PTR( ptr
, heapInfoArena
);
424 pArena
->prev
= start
| LOCAL_ARENA_FIXED
;
425 pArena
->next
= freeArena
;
427 /* Initialise the heap info structure */
429 pHeapInfo
= (LOCALHEAPINFO
*) (ptr
+ heapInfoArena
+ ARENA_HEADER_SIZE
);
430 memset( pHeapInfo
, 0, sizeof(LOCALHEAPINFO
) );
431 pHeapInfo
->items
= 4;
432 pHeapInfo
->first
= start
;
433 pHeapInfo
->last
= lastArena
;
434 pHeapInfo
->htable
= 0;
435 pHeapInfo
->hdelta
= 0x20;
436 pHeapInfo
->extra
= 0x200;
437 pHeapInfo
->minsize
= lastArena
- freeArena
;
438 pHeapInfo
->magic
= LOCAL_HEAP_MAGIC
;
440 /* Initialise the large free block */
442 pArena
= ARENA_PTR( ptr
, freeArena
);
443 pArena
->prev
= heapInfoArena
| LOCAL_ARENA_FREE
;
444 pArena
->next
= lastArena
;
445 pArena
->size
= lastArena
- freeArena
;
446 pArena
->free_prev
= start
;
447 pArena
->free_next
= lastArena
;
449 /* Initialise the last block */
451 pLastArena
= ARENA_PTR( ptr
, lastArena
);
452 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
453 pLastArena
->next
= lastArena
; /* this one */
454 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
455 pLastArena
->free_prev
= freeArena
;
456 pLastArena
->free_next
= lastArena
; /* this one */
458 /* Store the local heap address in the instance data */
460 ((INSTANCEDATA
*)ptr
)->heap
= heapInfoArena
+ ARENA_HEADER_SIZE
;
461 LOCAL_PrintHeap( selector
);
465 CURRENT_STACK16
->ecx
= ret
; /* must be returned in cx too */
469 /***********************************************************************
472 static BOOL16
LOCAL_GrowHeap( HANDLE16 ds
)
477 LOCALHEAPINFO
*pHeapInfo
;
478 WORD freeArena
, lastArena
;
479 LOCALARENA
*pArena
, *pLastArena
;
482 hseg
= GlobalHandle16( ds
);
483 /* maybe mem allocated by Virtual*() ? */
484 if (!hseg
) return FALSE
;
486 oldsize
= GlobalSize16( hseg
);
487 /* if nothing can be gained, return */
488 if (oldsize
> 0xfff0) return FALSE
;
489 hseg
= GlobalReAlloc16( hseg
, 0x10000, GMEM_FIXED
);
490 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
491 pHeapInfo
= LOCAL_GetHeap( ds
);
492 if (pHeapInfo
== NULL
) {
493 ERR("Heap not found\n" );
496 end
= GlobalSize16( hseg
);
497 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
499 /* Update the HeapInfo */
501 freeArena
= pHeapInfo
->last
;
502 pHeapInfo
->last
= lastArena
;
503 pHeapInfo
->minsize
+= end
- oldsize
;
505 /* grow the old last block */
506 pArena
= ARENA_PTR( ptr
, freeArena
);
507 pArena
->size
= lastArena
- freeArena
;
508 pArena
->next
= lastArena
;
509 pArena
->free_next
= lastArena
;
511 /* Initialise the new last block */
513 pLastArena
= ARENA_PTR( ptr
, lastArena
);
514 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
515 pLastArena
->next
= lastArena
; /* this one */
516 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
517 pLastArena
->free_prev
= freeArena
;
518 pLastArena
->free_next
= lastArena
; /* this one */
520 /* If block before freeArena is also free then merge them */
521 if((ARENA_PTR(ptr
, (pArena
->prev
& ~3))->prev
& 3) == LOCAL_ARENA_FREE
)
523 LOCAL_RemoveBlock(ptr
, freeArena
);
527 TRACE("Heap expanded\n" );
528 LOCAL_PrintHeap( ds
);
533 /***********************************************************************
536 static HLOCAL16
LOCAL_FreeArena( WORD ds
, WORD arena
)
538 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
539 LOCALHEAPINFO
*pInfo
;
540 LOCALARENA
*pArena
, *pPrev
, *pNext
;
542 TRACE("%04x ds=%04x\n", arena
, ds
);
543 if (!(pInfo
= LOCAL_GetHeap( ds
))) return arena
;
545 pArena
= ARENA_PTR( ptr
, arena
);
546 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
548 /* shouldn't happen */
549 ERR("Trying to free block %04x twice!\n",
551 LOCAL_PrintHeap( ds
);
555 /* Check if we can merge with the previous block */
557 pPrev
= ARENA_PTR( ptr
, pArena
->prev
& ~3 );
558 pNext
= ARENA_PTR( ptr
, pArena
->next
);
559 if ((pPrev
->prev
& 3) == LOCAL_ARENA_FREE
)
561 arena
= pArena
->prev
& ~3;
563 LOCAL_RemoveBlock( ptr
, pPrev
->next
);
566 else /* Make a new free block */
568 LOCAL_MakeBlockFree( ptr
, arena
);
571 /* Check if we can merge with the next block */
573 if ((pArena
->next
== pArena
->free_next
) &&
574 (pArena
->next
!= pInfo
->last
))
576 LOCAL_RemoveBlock( ptr
, pArena
->next
);
583 /***********************************************************************
586 * Shrink an arena by creating a free block at its end if possible.
587 * 'size' includes the arena header, and must be aligned.
589 static void LOCAL_ShrinkArena( WORD ds
, WORD arena
, WORD size
)
591 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
592 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
594 if (arena
+ size
+ LALIGN(sizeof(LOCALARENA
)) < pArena
->next
)
596 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
598 LOCAL_AddBlock( ptr
, arena
, arena
+ size
);
600 LOCAL_FreeArena( ds
, arena
+ size
);
605 /***********************************************************************
606 * LOCAL_GrowArenaDownward
608 * Grow an arena downward by using the previous arena (must be free).
610 static void LOCAL_GrowArenaDownward( WORD ds
, WORD arena
, WORD newsize
)
612 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
613 LOCALHEAPINFO
*pInfo
;
614 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
615 WORD prevArena
= pArena
->prev
& ~3;
616 LOCALARENA
*pPrevArena
= ARENA_PTR( ptr
, prevArena
);
620 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
621 offset
= pPrevArena
->size
;
622 size
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
623 LOCAL_RemoveFreeBlock( ptr
, prevArena
);
624 LOCAL_RemoveBlock( ptr
, arena
);
626 p
= (char *)pPrevArena
+ ARENA_HEADER_SIZE
;
627 while (offset
< size
)
629 memcpy( p
, p
+ offset
, offset
);
633 if (size
) memcpy( p
, p
+ offset
, size
);
634 LOCAL_ShrinkArena( ds
, prevArena
, newsize
);
639 /***********************************************************************
640 * LOCAL_GrowArenaUpward
642 * Grow an arena upward by using the next arena (must be free and big
643 * enough). Newsize includes the arena header and must be aligned.
645 static void LOCAL_GrowArenaUpward( WORD ds
, WORD arena
, WORD newsize
)
647 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
648 LOCALHEAPINFO
*pInfo
;
649 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
650 WORD nextArena
= pArena
->next
;
652 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
653 LOCAL_RemoveBlock( ptr
, nextArena
);
655 LOCAL_ShrinkArena( ds
, arena
, newsize
);
659 /***********************************************************************
662 static WORD
LOCAL_GetFreeSpace(WORD ds
, WORD countdiscard
)
664 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
665 LOCALHEAPINFO
*pInfo
;
670 if (!(pInfo
= LOCAL_GetHeap( ds
)))
672 ERR("Local heap not found\n" );
676 arena
= pInfo
->first
;
677 pArena
= ARENA_PTR( ptr
, arena
);
678 while (arena
!= pArena
->free_next
)
680 arena
= pArena
->free_next
;
681 pArena
= ARENA_PTR( ptr
, arena
);
682 if (pArena
->size
>= freespace
) freespace
= pArena
->size
;
684 /* FIXME doesn't yet calculate space that would become free if everything
685 were discarded when countdiscard == 1 */
686 if (freespace
< ARENA_HEADER_SIZE
) freespace
= 0;
687 else freespace
-= ARENA_HEADER_SIZE
;
692 /***********************************************************************
695 WORD
LOCAL_Compact( HANDLE16 ds
, UINT16 minfree
, UINT16 flags
)
697 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
698 LOCALHEAPINFO
*pInfo
;
699 LOCALARENA
*pArena
, *pMoveArena
, *pFinalArena
;
700 WORD arena
, movearena
, finalarena
, table
;
701 WORD count
, movesize
, size
;
703 LOCALHANDLEENTRY
*pEntry
;
705 if (!(pInfo
= LOCAL_GetHeap( ds
)))
707 ERR("Local heap not found\n" );
711 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
713 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
714 if(freespace
>= minfree
|| (flags
& LMEM_NOCOMPACT
))
716 TRACE("Returning %04x.\n", freespace
);
719 TRACE("Compacting heap %04x.\n", ds
);
720 table
= pInfo
->htable
;
723 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
724 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
726 if((pEntry
->lock
== 0) && (pEntry
->flags
!= (LMEM_DISCARDED
>> 8)))
728 /* OK we can move this one if we want */
729 TRACE("handle %04x (block %04x) can be moved.\n",
730 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
731 movearena
= ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
);
732 pMoveArena
= ARENA_PTR(ptr
, movearena
);
733 movesize
= pMoveArena
->next
- movearena
;
734 arena
= pInfo
->first
;
735 pArena
= ARENA_PTR(ptr
, arena
);
738 /* Try to find the smallest arena that will do, */
739 /* which is below us in memory */
742 arena
= pArena
->free_next
;
743 pArena
= ARENA_PTR(ptr
, arena
);
744 if(arena
>= movearena
)
746 if(arena
== pArena
->free_next
)
748 if((pArena
->size
>= movesize
) && (pArena
->size
< size
))
754 if (finalarena
) /* Actually got somewhere to move */
756 TRACE("Moving it to %04x.\n", finalarena
);
757 pFinalArena
= ARENA_PTR(ptr
, finalarena
);
758 size
= pFinalArena
->size
;
759 LOCAL_RemoveFreeBlock(ptr
, finalarena
);
760 LOCAL_ShrinkArena( ds
, finalarena
, movesize
);
761 /* Copy the arena to it's new location */
762 memcpy((char *)pFinalArena
+ ARENA_HEADER_SIZE
,
763 (char *)pMoveArena
+ ARENA_HEADER_SIZE
,
764 movesize
- ARENA_HEADER_SIZE
);
765 /* Free the old location */
766 LOCAL_FreeArena(ds
, movearena
);
768 LOCAL_CallTo16_word_www(pInfo
->notify
, LN_MOVE
,
769 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
770 /* Update handle table entry */
771 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ MOVEABLE_PREFIX
;
773 else if((ARENA_PTR(ptr
, pMoveArena
->prev
& ~3)->prev
& 3)
776 /* Previous arena is free (but < movesize) */
777 /* so we can 'slide' movearena down into it */
778 finalarena
= pMoveArena
->prev
& ~3;
779 LOCAL_GrowArenaDownward( ds
, movearena
, movesize
);
780 /* Update handle table entry */
781 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ MOVEABLE_PREFIX
;
785 table
= *(WORD
*)pEntry
;
787 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
788 if(freespace
>= minfree
|| (flags
& LMEM_NODISCARD
))
790 TRACE("Returning %04x.\n", freespace
);
794 table
= pInfo
->htable
;
797 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
798 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
800 if(pEntry
->addr
&& pEntry
->lock
== 0 &&
801 (pEntry
->flags
& (LMEM_DISCARDABLE
>> 8)))
803 TRACE("Discarding handle %04x (block %04x).\n",
804 (char *)pEntry
- ptr
, pEntry
->addr
);
805 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
));
807 LOCAL_CallTo16_word_www(pInfo
->notify
, LN_DISCARD
,
808 (char *)pEntry
- ptr
, pEntry
->flags
);
810 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
813 table
= *(WORD
*)pEntry
;
815 return LOCAL_Compact(ds
, 0xffff, LMEM_NODISCARD
);
819 /***********************************************************************
820 * LOCAL_FindFreeBlock
822 static HLOCAL16
LOCAL_FindFreeBlock( HANDLE16 ds
, WORD size
)
824 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
825 LOCALHEAPINFO
*pInfo
;
829 if (!(pInfo
= LOCAL_GetHeap( ds
)))
831 ERR("Local heap not found\n" );
836 arena
= pInfo
->first
;
837 pArena
= ARENA_PTR( ptr
, arena
);
839 arena
= pArena
->free_next
;
840 pArena
= ARENA_PTR( ptr
, arena
);
841 if (arena
== pArena
->free_next
) break;
842 if (pArena
->size
>= size
) return arena
;
844 TRACE("not enough space\n" );
850 /***********************************************************************
853 static const char *get_heap_name( WORD ds
)
855 HINSTANCE16 inst
= LoadLibrary16( "GDI" );
856 if (ds
== GlobalHandleToSel16( inst
))
858 FreeLibrary16( inst
);
861 FreeLibrary16( inst
);
862 inst
= LoadLibrary16( "USER" );
863 if (ds
== GlobalHandleToSel16( inst
))
865 FreeLibrary16( inst
);
868 FreeLibrary16( inst
);
872 /***********************************************************************
874 * The segment may get moved around in this function, so all callers
875 * should reset their pointer variables.
877 static HLOCAL16
LOCAL_GetBlock( HANDLE16 ds
, WORD size
, WORD flags
)
879 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
880 LOCALHEAPINFO
*pInfo
;
884 if (!(pInfo
= LOCAL_GetHeap( ds
)))
886 ERR("Local heap not found\n");
891 size
+= ARENA_HEADER_SIZE
;
892 size
= LALIGN( max( size
, sizeof(LOCALARENA
) ) );
897 /* Find a suitable free block */
898 arena
= LOCAL_FindFreeBlock( ds
, size
);
900 /* no space: try to make some */
901 LOCAL_Compact( ds
, size
, flags
);
902 arena
= LOCAL_FindFreeBlock( ds
, size
);
905 /* still no space: try to grow the segment */
906 if (!(LOCAL_GrowHeap( ds
)))
909 /* FIXME: doesn't work correctly yet */
910 if ((pInfo
->notify
) && (LOCAL_CallTo16_word_www(pInfo
->notify
, LN_OUTOFMEM
, ds
- 20, size
))) /* FIXME: "size" correct ? (should indicate bytes needed) */
915 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
916 pInfo
= LOCAL_GetHeap( ds
);
917 arena
= LOCAL_FindFreeBlock( ds
, size
);
920 ERR( "not enough space in %s heap %04x for %d bytes\n",
921 get_heap_name(ds
), ds
, size
);
923 if ((pInfo
->notify
) &&
924 /* FIXME: "size" correct ? (should indicate bytes needed) */
925 (LOCAL_CallTo16_word_www(pInfo
->notify
, LN_OUTOFMEM
, ds
, size
)))
931 /* Make a block out of the free arena */
932 pArena
= ARENA_PTR( ptr
, arena
);
933 TRACE("size = %04x, arena %04x size %04x\n",
934 size
, arena
, pArena
->size
);
935 LOCAL_RemoveFreeBlock( ptr
, arena
);
936 LOCAL_ShrinkArena( ds
, arena
, size
);
938 if (flags
& LMEM_ZEROINIT
)
939 memset((char *)pArena
+ ARENA_HEADER_SIZE
, 0, size
-ARENA_HEADER_SIZE
);
940 return arena
+ ARENA_HEADER_SIZE
;
944 /***********************************************************************
947 static BOOL16
LOCAL_NewHTable( HANDLE16 ds
)
949 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
950 LOCALHEAPINFO
*pInfo
;
951 LOCALHANDLEENTRY
*pEntry
;
956 if (!(pInfo
= LOCAL_GetHeap( ds
)))
958 ERR("Local heap not found\n");
963 if (!(handle
= LOCAL_GetBlock( ds
, pInfo
->hdelta
* sizeof(LOCALHANDLEENTRY
)
964 + 2 * sizeof(WORD
), LMEM_FIXED
)))
966 if (!(ptr
= MapSL( MAKESEGPTR( ds
, 0 ) )))
967 ERR("ptr == NULL after GetBlock.\n");
968 if (!(pInfo
= LOCAL_GetHeap( ds
)))
969 ERR("pInfo == NULL after GetBlock.\n");
971 /* Fill the entry table */
973 *(WORD
*)(ptr
+ handle
) = pInfo
->hdelta
;
974 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
+ sizeof(WORD
));
975 for (i
= pInfo
->hdelta
; i
> 0; i
--, pEntry
++) {
976 pEntry
->lock
= pEntry
->flags
= 0xff;
979 *(WORD
*)pEntry
= pInfo
->htable
;
980 pInfo
->htable
= handle
;
985 /***********************************************************************
986 * LOCAL_GetNewHandleEntry
988 static HLOCAL16
LOCAL_GetNewHandleEntry( HANDLE16 ds
)
990 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
991 LOCALHEAPINFO
*pInfo
;
992 LOCALHANDLEENTRY
*pEntry
= NULL
;
995 if (!(pInfo
= LOCAL_GetHeap( ds
)))
997 ERR("Local heap not found\n");
1002 /* Find a free slot in existing tables */
1004 table
= pInfo
->htable
;
1007 WORD count
= *(WORD
*)(ptr
+ table
);
1008 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1009 for (; count
> 0; count
--, pEntry
++)
1010 if (pEntry
->lock
== 0xff) break;
1012 table
= *(WORD
*)pEntry
;
1015 if (!table
) /* We need to create a new table */
1017 if (!LOCAL_NewHTable( ds
)) return 0;
1018 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1019 pInfo
= LOCAL_GetHeap( ds
);
1020 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ pInfo
->htable
+ sizeof(WORD
));
1023 /* Now allocate this entry */
1027 TRACE("(%04x): %04x\n",
1028 ds
, ((char *)pEntry
- ptr
) );
1029 return (HLOCAL16
)((char *)pEntry
- ptr
);
1033 /***********************************************************************
1034 * LOCAL_FreeHandleEntry
1036 * Free a handle table entry.
1038 static void LOCAL_FreeHandleEntry( HANDLE16 ds
, HLOCAL16 handle
)
1040 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1041 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1042 LOCALHEAPINFO
*pInfo
;
1044 WORD table
, count
, i
;
1046 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
1048 /* Find the table where this handle comes from */
1050 pTable
= &pInfo
->htable
;
1053 WORD size
= (*(WORD
*)(ptr
+ *pTable
)) * sizeof(LOCALHANDLEENTRY
);
1054 if ((handle
>= *pTable
+ sizeof(WORD
)) &&
1055 (handle
< *pTable
+ sizeof(WORD
) + size
)) break; /* Found it */
1056 pTable
= (WORD
*)(ptr
+ *pTable
+ sizeof(WORD
) + size
);
1060 ERR("Invalid entry %04x\n", handle
);
1061 LOCAL_PrintHeap( ds
);
1065 /* Make the entry free */
1067 pEntry
->addr
= 0; /* just in case */
1068 pEntry
->lock
= 0xff;
1069 pEntry
->flags
= 0xff;
1070 /* Now check if all entries in this table are free */
1073 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1074 count
= *(WORD
*)(ptr
+ table
);
1075 for (i
= count
; i
> 0; i
--, pEntry
++) if (pEntry
->lock
!= 0xff) return;
1077 /* Remove the table from the linked list and free it */
1079 TRACE("(%04x): freeing table %04x\n",
1081 *pTable
= *(WORD
*)pEntry
;
1082 LOCAL_FreeArena( ds
, ARENA_HEADER( table
) );
1086 /***********************************************************************
1089 * Implementation of LocalFree().
1091 HLOCAL16
LOCAL_Free( HANDLE16 ds
, HLOCAL16 handle
)
1093 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1095 TRACE("%04x ds=%04x\n", handle
, ds
);
1097 if (!handle
) { WARN("Handle is 0.\n" ); return 0; }
1098 if (HANDLE_FIXED( handle
))
1100 if (!LOCAL_FreeArena( ds
, ARENA_HEADER( handle
) )) return 0; /* OK */
1101 else return handle
; /* couldn't free it */
1105 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1106 if (pEntry
->flags
!= (LMEM_DISCARDED
>> 8))
1108 TRACE("real block at %04x\n",
1110 if (LOCAL_FreeArena( ds
, ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
) ))
1111 return handle
; /* couldn't free it */
1113 LOCAL_FreeHandleEntry( ds
, handle
);
1119 /***********************************************************************
1122 * Implementation of LocalAlloc().
1125 HLOCAL16
LOCAL_Alloc( HANDLE16 ds
, WORD flags
, WORD size
)
1130 TRACE("%04x %d ds=%04x\n", flags
, size
, ds
);
1132 if(size
> 0 && size
<= 4) size
= 5;
1133 if (flags
& LMEM_MOVEABLE
)
1135 LOCALHANDLEENTRY
*plhe
;
1140 if (!(hmem
= LOCAL_GetBlock( ds
, size
+ MOVEABLE_PREFIX
, flags
)))
1143 else /* We just need to allocate a discarded handle */
1145 if (!(handle
= LOCAL_GetNewHandleEntry( ds
)))
1147 WARN("Couldn't get handle.\n");
1149 LOCAL_FreeArena( ds
, ARENA_HEADER(hmem
) );
1152 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1153 plhe
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1157 plhe
->addr
= hmem
+ MOVEABLE_PREFIX
;
1158 plhe
->flags
= (BYTE
)((flags
& 0x0f00) >> 8);
1159 *(HLOCAL16
*)(ptr
+ hmem
) = handle
;
1164 plhe
->flags
= LMEM_DISCARDED
>> 8;
1171 handle
= LOCAL_GetBlock( ds
, size
, flags
);
1177 /***********************************************************************
1180 * Implementation of LocalReAlloc().
1182 HLOCAL16
LOCAL_ReAlloc( HANDLE16 ds
, HLOCAL16 handle
, WORD size
, WORD flags
)
1184 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1185 LOCALHEAPINFO
*pInfo
;
1186 LOCALARENA
*pArena
, *pNext
;
1187 LOCALHANDLEENTRY
*pEntry
= NULL
;
1188 WORD arena
, oldsize
;
1189 HLOCAL16 hmem
, blockhandle
;
1192 if (!handle
) return 0;
1193 if(HANDLE_MOVEABLE(handle
) &&
1194 ((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
== 0xff) /* An unused handle */
1197 TRACE("%04x %d %04x ds=%04x\n",
1198 handle
, size
, flags
, ds
);
1199 if (!(pInfo
= LOCAL_GetHeap( ds
))) return 0;
1201 if (HANDLE_FIXED( handle
))
1202 blockhandle
= handle
;
1205 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1206 if(pEntry
->flags
== (LMEM_DISCARDED
>> 8))
1210 WARN("Dicarded block has non-zero addr.\n");
1211 TRACE("ReAllocating discarded block\n");
1212 if(size
<= 4) size
= 5;
1213 if (!(hl
= LOCAL_GetBlock( ds
, size
+ MOVEABLE_PREFIX
, flags
)))
1215 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) ); /* Reload ptr */
1216 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1217 pEntry
->addr
= hl
+ MOVEABLE_PREFIX
;
1220 *(HLOCAL16
*)(ptr
+ hl
) = handle
;
1223 if (((blockhandle
= pEntry
->addr
- MOVEABLE_PREFIX
) & 3) != 0)
1225 ERR("(%04x,%04x): invalid handle\n",
1229 if (*(HLOCAL16
*)(ptr
+ blockhandle
) != handle
) {
1230 ERR("Back ptr to handle is invalid\n");
1235 if (flags
& LMEM_MODIFY
)
1237 if (HANDLE_MOVEABLE(handle
))
1239 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1240 pEntry
->flags
= (flags
& 0x0f00) >> 8;
1241 TRACE("Changing flags to %x.\n", pEntry
->flags
);
1248 if (flags
& LMEM_MOVEABLE
)
1250 if (HANDLE_FIXED(handle
))
1252 TRACE("Freeing fixed block.\n");
1253 return LOCAL_Free( ds
, handle
);
1255 else /* Moveable block */
1257 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1258 if (pEntry
->lock
== 0)
1260 /* discards moveable blocks */
1261 TRACE("Discarding block\n");
1262 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
));
1264 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
1272 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1273 if (pEntry
->lock
== 0)
1276 return LOCAL_Free( ds
, handle
);
1282 arena
= ARENA_HEADER( blockhandle
);
1283 TRACE("arena is %04x\n", arena
);
1284 pArena
= ARENA_PTR( ptr
, arena
);
1286 if(size
<= 4) size
= 5;
1287 if(HANDLE_MOVEABLE(handle
)) size
+= MOVEABLE_PREFIX
;
1288 oldsize
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
1289 nextarena
= LALIGN(blockhandle
+ size
);
1291 /* Check for size reduction */
1293 if (nextarena
<= pArena
->next
)
1295 TRACE("size reduction, making new free block\n");
1296 LOCAL_ShrinkArena(ds
, arena
, nextarena
- arena
);
1297 TRACE("returning %04x\n", handle
);
1301 /* Check if the next block is free and large enough */
1303 pNext
= ARENA_PTR( ptr
, pArena
->next
);
1304 if (((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) &&
1305 (nextarena
<= pNext
->next
))
1307 TRACE("size increase, making new free block\n");
1308 LOCAL_GrowArenaUpward(ds
, arena
, nextarena
- arena
);
1309 TRACE("returning %04x\n", handle
);
1313 /* Now we have to allocate a new block, but not if (fixed block or locked
1314 block) and no LMEM_MOVEABLE */
1316 if (!(flags
& LMEM_MOVEABLE
))
1318 if (HANDLE_FIXED(handle
))
1320 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1325 if(((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
!= 0)
1327 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1333 hmem
= LOCAL_GetBlock( ds
, size
, flags
);
1334 ptr
= MapSL( MAKESEGPTR( ds
, 0 )); /* Reload ptr */
1335 if(HANDLE_MOVEABLE(handle
)) /* LOCAL_GetBlock might have triggered */
1336 { /* a compaction, which might in turn have */
1337 blockhandle
= pEntry
->addr
- MOVEABLE_PREFIX
; /* moved the very block we are resizing */
1338 arena
= ARENA_HEADER( blockhandle
); /* thus, we reload arena, too */
1342 /* Remove the block from the heap and try again */
1343 LPSTR buffer
= HeapAlloc( GetProcessHeap(), 0, oldsize
);
1344 if (!buffer
) return 0;
1345 memcpy( buffer
, ptr
+ arena
+ ARENA_HEADER_SIZE
, oldsize
);
1346 LOCAL_FreeArena( ds
, arena
);
1347 if (!(hmem
= LOCAL_GetBlock( ds
, size
, flags
)))
1349 if (!(hmem
= LOCAL_GetBlock( ds
, oldsize
, flags
)))
1351 ERR("Can't restore saved block\n" );
1352 HeapFree( GetProcessHeap(), 0, buffer
);
1357 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) ); /* Reload ptr */
1358 memcpy( ptr
+ hmem
, buffer
, oldsize
);
1359 HeapFree( GetProcessHeap(), 0, buffer
);
1363 memcpy( ptr
+ hmem
, ptr
+ (arena
+ ARENA_HEADER_SIZE
), oldsize
);
1364 LOCAL_FreeArena( ds
, arena
);
1366 if (HANDLE_MOVEABLE( handle
))
1368 TRACE("fixing handle\n");
1369 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1370 pEntry
->addr
= hmem
+ MOVEABLE_PREFIX
;
1371 /* Back ptr should still be correct */
1372 if(*(HLOCAL16
*)(ptr
+ hmem
) != handle
)
1373 ERR("back ptr is invalid.\n");
1376 if (size
== oldsize
) hmem
= 0; /* Realloc failed */
1377 TRACE("returning %04x\n", hmem
);
1382 /***********************************************************************
1383 * LOCAL_InternalLock
1385 static HLOCAL16
LOCAL_InternalLock( LPSTR heap
, HLOCAL16 handle
)
1387 HLOCAL16 old_handle
= handle
;
1389 if (HANDLE_MOVEABLE(handle
))
1391 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(heap
+ handle
);
1392 if (pEntry
->flags
== LMEM_DISCARDED
) return 0;
1393 if (pEntry
->lock
< 0xfe) pEntry
->lock
++;
1394 handle
= pEntry
->addr
;
1396 TRACE("%04x returning %04x\n",
1397 old_handle
, handle
);
1402 /***********************************************************************
1405 void *LOCAL_Lock( HANDLE16 ds
, HLOCAL16 handle
)
1407 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1408 return handle
? ptr
+ LOCAL_InternalLock( ptr
, handle
) : NULL
;
1412 /***********************************************************************
1415 BOOL16
LOCAL_Unlock( HANDLE16 ds
, HLOCAL16 handle
)
1417 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1419 TRACE("%04x\n", handle
);
1420 if (HANDLE_MOVEABLE(handle
))
1422 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1423 if (!pEntry
->lock
|| (pEntry
->lock
== 0xff)) return FALSE
;
1424 /* For moveable block, return the new lock count */
1425 /* (see _Windows_Internals_ p. 197) */
1426 return --pEntry
->lock
;
1432 /***********************************************************************
1435 * Implementation of LocalSize().
1437 WORD
LOCAL_Size( HANDLE16 ds
, HLOCAL16 handle
)
1439 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1442 TRACE("%04x ds=%04x\n", handle
, ds
);
1444 if (!handle
) return 0;
1445 if (HANDLE_MOVEABLE( handle
))
1447 handle
= *(WORD
*)(ptr
+ handle
);
1448 if (!handle
) return 0;
1449 pArena
= ARENA_PTR( ptr
, ARENA_HEADER(handle
- MOVEABLE_PREFIX
) );
1452 pArena
= ARENA_PTR( ptr
, ARENA_HEADER(handle
) );
1454 return pArena
->next
- handle
;
1458 /***********************************************************************
1461 * Implementation of LocalFlags().
1463 WORD
LOCAL_Flags( HANDLE16 ds
, HLOCAL16 handle
)
1465 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1467 if (HANDLE_MOVEABLE(handle
))
1469 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1470 TRACE("(%04x,%04x): returning %04x\n",
1471 ds
, handle
, pEntry
->lock
| (pEntry
->flags
<< 8) );
1472 return pEntry
->lock
| (pEntry
->flags
<< 8);
1476 TRACE("(%04x,%04x): returning 0\n",
1483 /***********************************************************************
1486 * Implementation of LocalHeapSize().
1488 WORD
LOCAL_HeapSize( HANDLE16 ds
)
1490 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1491 if (!pInfo
) return 0;
1492 return pInfo
->last
- pInfo
->first
;
1496 /***********************************************************************
1499 * Implementation of LocalCountFree().
1501 WORD
LOCAL_CountFree( HANDLE16 ds
)
1505 LOCALHEAPINFO
*pInfo
;
1506 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1508 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1510 ERR("(%04x): Local heap not found\n", ds
);
1511 LOCAL_PrintHeap( ds
);
1516 arena
= pInfo
->first
;
1517 pArena
= ARENA_PTR( ptr
, arena
);
1520 arena
= pArena
->free_next
;
1521 pArena
= ARENA_PTR( ptr
, arena
);
1522 if (arena
== pArena
->free_next
) break;
1523 total
+= pArena
->size
;
1525 TRACE("(%04x): returning %d\n", ds
, total
);
1530 /***********************************************************************
1533 * Implementation of LocalHandle().
1535 HLOCAL16
LOCAL_Handle( HANDLE16 ds
, WORD addr
)
1537 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1538 LOCALHEAPINFO
*pInfo
;
1541 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1543 ERR("(%04x): Local heap not found\n", ds
);
1544 LOCAL_PrintHeap( ds
);
1548 /* Find the address in the entry tables */
1550 table
= pInfo
->htable
;
1553 WORD count
= *(WORD
*)(ptr
+ table
);
1554 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+table
+sizeof(WORD
));
1555 for (; count
> 0; count
--, pEntry
++)
1556 if (pEntry
->addr
== addr
) return (HLOCAL16
)((char *)pEntry
- ptr
);
1557 table
= *(WORD
*)pEntry
;
1560 return (HLOCAL16
)addr
; /* Fixed block handle is addr */
1564 /***********************************************************************
1565 * LocalAlloc16 (KERNEL.5)
1567 HLOCAL16 WINAPI
LocalAlloc16( UINT16 flags
, WORD size
)
1569 HLOCAL16 ret
= LOCAL_Alloc( CURRENT_DS
, flags
, size
);
1570 CURRENT_STACK16
->ecx
= ret
; /* must be returned in cx too */
1575 /***********************************************************************
1576 * LocalReAlloc16 (KERNEL.6)
1578 HLOCAL16 WINAPI
LocalReAlloc16( HLOCAL16 handle
, WORD size
, UINT16 flags
)
1580 return LOCAL_ReAlloc( CURRENT_DS
, handle
, size
, flags
);
1584 /***********************************************************************
1585 * LocalFree16 (KERNEL.7)
1587 HLOCAL16 WINAPI
LocalFree16( HLOCAL16 handle
)
1589 return LOCAL_Free( CURRENT_DS
, handle
);
1593 /***********************************************************************
1594 * LocalLock16 (KERNEL.8)
1596 * Note: only the offset part of the pointer is returned by the relay code.
1598 SEGPTR WINAPI
LocalLock16( HLOCAL16 handle
)
1600 WORD ds
= CURRENT_DS
;
1601 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1602 return MAKESEGPTR( ds
, LOCAL_InternalLock( ptr
, handle
) );
1606 /***********************************************************************
1607 * LocalUnlock16 (KERNEL.9)
1609 BOOL16 WINAPI
LocalUnlock16( HLOCAL16 handle
)
1611 return LOCAL_Unlock( CURRENT_DS
, handle
);
1615 /***********************************************************************
1616 * LocalSize16 (KERNEL.10)
1618 UINT16 WINAPI
LocalSize16( HLOCAL16 handle
)
1620 return LOCAL_Size( CURRENT_DS
, handle
);
1624 /***********************************************************************
1625 * LocalHandle16 (KERNEL.11)
1627 HLOCAL16 WINAPI
LocalHandle16( WORD addr
)
1629 return LOCAL_Handle( CURRENT_DS
, addr
);
1633 /***********************************************************************
1634 * LocalFlags16 (KERNEL.12)
1636 UINT16 WINAPI
LocalFlags16( HLOCAL16 handle
)
1638 return LOCAL_Flags( CURRENT_DS
, handle
);
1642 /***********************************************************************
1643 * LocalCompact16 (KERNEL.13)
1645 UINT16 WINAPI
LocalCompact16( UINT16 minfree
)
1647 TRACE("%04x\n", minfree
);
1648 return LOCAL_Compact( CURRENT_DS
, minfree
, 0 );
1652 /***********************************************************************
1653 * LocalNotify (KERNEL.14)
1655 * Installs a callback function that is called for local memory events
1656 * Callback function prototype is
1657 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1660 * NotifyFunc seems to be responsible for allocating some memory,
1661 * returns TRUE for success.
1662 * wArg = number of bytes needed additionally
1664 * hMem = handle; wArg = old mem location
1666 * NotifyFunc seems to be strongly encouraged to return TRUE,
1667 * otherwise LogError() gets called.
1668 * hMem = handle; wArg = flags
1670 FARPROC16 WINAPI
LocalNotify16( FARPROC16 func
)
1672 LOCALHEAPINFO
*pInfo
;
1673 FARPROC16 oldNotify
;
1674 HANDLE16 ds
= CURRENT_DS
;
1676 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1678 ERR("(%04x): Local heap not found\n", ds
);
1679 LOCAL_PrintHeap( ds
);
1682 TRACE("(%04x): %08lx\n", ds
, (DWORD
)func
);
1683 FIXME("Half implemented\n");
1684 oldNotify
= pInfo
->notify
;
1685 pInfo
->notify
= func
;
1690 /***********************************************************************
1691 * LocalShrink16 (KERNEL.121)
1693 UINT16 WINAPI
LocalShrink16( HGLOBAL16 handle
, UINT16 newsize
)
1695 TRACE("%04x %04x\n", handle
, newsize
);
1700 /***********************************************************************
1701 * GetHeapSpaces (KERNEL.138)
1703 DWORD WINAPI
GetHeapSpaces16( HMODULE16 module
)
1708 if (!(pModule
= NE_GetPtr( module
))) return 0;
1710 GlobalHandleToSel16((NE_SEG_TABLE( pModule
) + pModule
->dgroup
- 1)->hSeg
);
1711 return MAKELONG( LOCAL_CountFree( ds
), LOCAL_HeapSize( ds
) );
1715 /***********************************************************************
1716 * LocalCountFree (KERNEL.161)
1718 WORD WINAPI
LocalCountFree16(void)
1720 return LOCAL_CountFree( CURRENT_DS
);
1724 /***********************************************************************
1725 * LocalHeapSize (KERNEL.162)
1727 WORD WINAPI
LocalHeapSize16(void)
1730 return LOCAL_HeapSize( CURRENT_DS
);
1734 /***********************************************************************
1735 * LocalHandleDelta (KERNEL.310)
1737 WORD WINAPI
LocalHandleDelta16( WORD delta
)
1739 LOCALHEAPINFO
*pInfo
;
1741 if (!(pInfo
= LOCAL_GetHeap( CURRENT_DS
)))
1743 ERR("Local heap not found\n");
1744 LOCAL_PrintHeap( CURRENT_DS
);
1747 if (delta
) pInfo
->hdelta
= delta
;
1748 TRACE("returning %04x\n", pInfo
->hdelta
);
1749 return pInfo
->hdelta
;
1753 /***********************************************************************
1754 * LocalInfo (TOOLHELP.56)
1756 BOOL16 WINAPI
LocalInfo16( LOCALINFO
*pLocalInfo
, HGLOBAL16 handle
)
1758 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle
)));
1759 if (!pInfo
) return FALSE
;
1760 pLocalInfo
->wcItems
= pInfo
->items
;
1765 /***********************************************************************
1766 * LocalFirst (TOOLHELP.57)
1768 BOOL16 WINAPI
LocalFirst16( LOCALENTRY
*pLocalEntry
, HGLOBAL16 handle
)
1770 WORD ds
= GlobalHandleToSel16( handle
);
1771 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1772 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1773 if (!pInfo
) return FALSE
;
1775 pLocalEntry
->hHandle
= pInfo
->first
+ ARENA_HEADER_SIZE
;
1776 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1777 pLocalEntry
->wFlags
= LF_FIXED
;
1778 pLocalEntry
->wcLock
= 0;
1779 pLocalEntry
->wType
= LT_NORMAL
;
1780 pLocalEntry
->hHeap
= handle
;
1781 pLocalEntry
->wHeapType
= NORMAL_HEAP
;
1782 pLocalEntry
->wNext
= ARENA_PTR(ptr
,pInfo
->first
)->next
;
1783 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1788 /***********************************************************************
1789 * LocalNext (TOOLHELP.58)
1791 BOOL16 WINAPI
LocalNext16( LOCALENTRY
*pLocalEntry
)
1793 WORD ds
= GlobalHandleToSel16( pLocalEntry
->hHeap
);
1794 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1797 if (!LOCAL_GetHeap( ds
)) return FALSE
;
1798 if (!pLocalEntry
->wNext
) return FALSE
;
1799 pArena
= ARENA_PTR( ptr
, pLocalEntry
->wNext
);
1801 pLocalEntry
->hHandle
= pLocalEntry
->wNext
+ ARENA_HEADER_SIZE
;
1802 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1803 pLocalEntry
->wFlags
= (pArena
->prev
& 3) + 1;
1804 pLocalEntry
->wcLock
= 0;
1805 pLocalEntry
->wType
= LT_NORMAL
;
1806 if (pArena
->next
!= pLocalEntry
->wNext
) /* last one? */
1807 pLocalEntry
->wNext
= pArena
->next
;
1809 pLocalEntry
->wNext
= 0;
1810 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1815 /***********************************************************************
1816 * LocalAlloc (KERNEL32.371)
1821 HLOCAL WINAPI
LocalAlloc(
1822 UINT flags
, /* [in] Allocation attributes */
1823 DWORD size
/* [in] Number of bytes to allocate */
1825 return (HLOCAL
)GlobalAlloc( flags
, size
);
1829 /***********************************************************************
1830 * LocalCompact (KERNEL32.372)
1832 UINT WINAPI
LocalCompact( UINT minfree
)
1834 return 0; /* LocalCompact does nothing in Win32 */
1838 /***********************************************************************
1839 * LocalFlags (KERNEL32.374)
1841 * Value specifying allocation flags and lock count.
1842 * LMEM_INVALID_HANDLE: Failure
1844 UINT WINAPI
LocalFlags(
1845 HLOCAL handle
/* [in] Handle of memory object */
1847 return GlobalFlags( (HGLOBAL
)handle
);
1851 /***********************************************************************
1852 * LocalFree (KERNEL32.375)
1857 HLOCAL WINAPI
LocalFree(
1858 HLOCAL handle
/* [in] Handle of memory object */
1860 return (HLOCAL
)GlobalFree( (HGLOBAL
)handle
);
1864 /***********************************************************************
1865 * LocalHandle (KERNEL32.376)
1870 HLOCAL WINAPI
LocalHandle(
1871 LPCVOID ptr
/* [in] Address of local memory object */
1873 return (HLOCAL
)GlobalHandle( ptr
);
1877 /***********************************************************************
1878 * LocalLock (KERNEL32.377)
1879 * Locks a local memory object and returns pointer to the first byte
1880 * of the memory block.
1886 LPVOID WINAPI
LocalLock(
1887 HLOCAL handle
/* [in] Address of local memory object */
1889 return GlobalLock( (HGLOBAL
)handle
);
1893 /***********************************************************************
1894 * LocalReAlloc (KERNEL32.378)
1899 HLOCAL WINAPI
LocalReAlloc(
1900 HLOCAL handle
, /* [in] Handle of memory object */
1901 DWORD size
, /* [in] New size of block */
1902 UINT flags
/* [in] How to reallocate object */
1904 return (HLOCAL
)GlobalReAlloc( (HGLOBAL
)handle
, size
, flags
);
1908 /***********************************************************************
1909 * LocalShrink (KERNEL32.379)
1911 UINT WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
1913 return 0; /* LocalShrink does nothing in Win32 */
1917 /***********************************************************************
1918 * LocalSize (KERNEL32.380)
1923 UINT WINAPI
LocalSize(
1924 HLOCAL handle
/* [in] Handle of memory object */
1926 return GlobalSize( (HGLOBAL
)handle
);
1930 /***********************************************************************
1931 * LocalUnlock (KERNEL32.381)
1933 * TRUE: Object is still locked
1934 * FALSE: Object is unlocked
1936 BOOL WINAPI
LocalUnlock(
1937 HLOCAL handle
/* [in] Handle of memory object */
1939 return GlobalUnlock( (HGLOBAL
)handle
);