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.
17 #include "wine/winbase16.h"
23 #include "stackframe.h"
25 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(local
);
29 DECLARE_DEBUG_CHANNEL(heap
);
34 WORD prev
; /* Previous arena | arena type */
35 WORD next
; /* Next arena */
36 /* Start of the memory block or free-list info */
37 WORD size
; /* Size of the free block */
38 WORD free_prev
; /* Previous free block */
39 WORD free_next
; /* Next free block */
42 #define ARENA_HEADER_SIZE 4
43 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
45 /* Arena types (stored in 'prev' field of the arena) */
46 #define LOCAL_ARENA_FREE 0
47 #define LOCAL_ARENA_FIXED 1
49 /* LocalNotify() msgs */
55 /* Layout of a handle entry table
57 * WORD count of entries
58 * LOCALHANDLEENTRY[count] entries
59 * WORD near ptr to next table
63 WORD addr
; /* Address of the MOVEABLE block */
64 BYTE flags
; /* Flags for this block */
65 BYTE lock
; /* Lock count */
69 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
70 * in case something actually relies on this.
71 * Note the ARENA_HEADER(addr) still produces the desired result ie. 4n - 4
73 * An unused handle has lock = flags = 0xff. In windows addr is that of next
74 * free handle, at the moment in wine we set it to 0.
76 * A discarded block's handle has lock = addr = 0 and flags = 0x40
77 * (LMEM_DISCARDED >> 8)
84 WORD check
; /* 00 Heap checking flag */
85 WORD freeze
; /* 02 Heap frozen flag */
86 WORD items
; /* 04 Count of items on the heap */
87 WORD first
; /* 06 First item of the heap */
88 WORD pad1
; /* 08 Always 0 */
89 WORD last
; /* 0a Last item of the heap */
90 WORD pad2
; /* 0c Always 0 */
91 BYTE ncompact
; /* 0e Compactions counter */
92 BYTE dislevel
; /* 0f Discard level */
93 DWORD distotal
; /* 10 Total bytes discarded */
94 WORD htable
; /* 14 Pointer to handle table */
95 WORD hfree
; /* 16 Pointer to free handle table */
96 WORD hdelta
; /* 18 Delta to expand the handle table */
97 WORD expand
; /* 1a Pointer to expand function (unused) */
98 WORD pstat
; /* 1c Pointer to status structure (unused) */
99 FARPROC16 notify WINE_PACKED
; /* 1e Pointer to LocalNotify() function */
100 WORD lock
; /* 22 Lock count for the heap */
101 WORD extra
; /* 24 Extra bytes to allocate when expanding */
102 WORD minsize
; /* 26 Minimum size of the heap */
103 WORD magic
; /* 28 Magic number */
108 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
110 WORD USER_HeapSel
= 0; /* USER heap selector */
112 /* All local heap allocations are aligned on 4-byte boundaries */
113 #define LALIGN(word) (((word) + 3) & ~3)
115 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
116 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
117 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
118 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
120 /* determine whether the handle belongs to a fixed or a moveable block */
121 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
122 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
125 /* ### start build ### */
126 extern WORD CALLBACK
LOCAL_CallTo16_word_www(FARPROC16
,WORD
,HLOCAL16
,WORD
);
127 /* ### stop build ### */
129 /***********************************************************************
132 * Return a pointer to the local heap, making sure it exists.
134 static LOCALHEAPINFO
*LOCAL_GetHeap( HANDLE16 ds
)
136 LOCALHEAPINFO
*pInfo
;
137 INSTANCEDATA
*ptr
= MapSL( MAKESEGPTR( ds
, 0 ));
138 TRACE("Heap at %p, %04x\n", ptr
, (ptr
!= NULL
? ptr
->heap
: 0xFFFF));
139 if (!ptr
|| !ptr
->heap
) return NULL
;
140 if (IsBadReadPtr16( (SEGPTR
)MAKELONG(ptr
->heap
,ds
), sizeof(LOCALHEAPINFO
)))
142 WARN("Bad pointer\n");
145 pInfo
= (LOCALHEAPINFO
*)((char*)ptr
+ ptr
->heap
);
146 if (pInfo
->magic
!= LOCAL_HEAP_MAGIC
)
155 /***********************************************************************
156 * LOCAL_MakeBlockFree
158 * Make a block free, inserting it in the free-list.
159 * 'block' is the handle of the block arena; 'baseptr' points to
160 * the beginning of the data segment containing the heap.
162 static void LOCAL_MakeBlockFree( char *baseptr
, WORD block
)
164 LOCALARENA
*pArena
, *pNext
;
167 /* Mark the block as free */
169 pArena
= ARENA_PTR( baseptr
, block
);
170 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FREE
;
171 pArena
->size
= pArena
->next
- block
;
173 /* Find the next free block (last block is always free) */
178 pNext
= ARENA_PTR( baseptr
, next
);
179 if ((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) break;
183 TRACE("%04x, next %04x\n", block
, next
);
184 /* Insert the free block in the free-list */
186 pArena
->free_prev
= pNext
->free_prev
;
187 pArena
->free_next
= next
;
188 ARENA_PTR(baseptr
,pNext
->free_prev
)->free_next
= block
;
189 pNext
->free_prev
= block
;
193 /***********************************************************************
194 * LOCAL_RemoveFreeBlock
196 * Remove a block from the free-list.
197 * 'block' is the handle of the block arena; 'baseptr' points to
198 * the beginning of the data segment containing the heap.
200 static void LOCAL_RemoveFreeBlock( char *baseptr
, WORD block
)
202 /* Mark the block as fixed */
204 LOCALARENA
*pArena
= ARENA_PTR( baseptr
, block
);
205 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FIXED
;
207 /* Remove it from the list */
209 ARENA_PTR(baseptr
,pArena
->free_prev
)->free_next
= pArena
->free_next
;
210 ARENA_PTR(baseptr
,pArena
->free_next
)->free_prev
= pArena
->free_prev
;
214 /***********************************************************************
217 * Insert a new block in the heap.
218 * 'new' is the handle of the new block arena; 'baseptr' points to
219 * the beginning of the data segment containing the heap; 'prev' is
220 * the block before the new one.
222 static void LOCAL_AddBlock( char *baseptr
, WORD prev
, WORD
new )
224 LOCALARENA
*pPrev
= ARENA_PTR( baseptr
, prev
);
225 LOCALARENA
*pNew
= ARENA_PTR( baseptr
, new );
227 pNew
->prev
= (prev
& ~3) | LOCAL_ARENA_FIXED
;
228 pNew
->next
= pPrev
->next
;
229 ARENA_PTR(baseptr
,pPrev
->next
)->prev
&= 3;
230 ARENA_PTR(baseptr
,pPrev
->next
)->prev
|= new;
235 /***********************************************************************
238 * Remove a block from the heap.
239 * 'block' is the handle of the block arena; 'baseptr' points to
240 * the beginning of the data segment containing the heap.
242 static void LOCAL_RemoveBlock( char *baseptr
, WORD block
)
244 LOCALARENA
*pArena
, *pTmp
;
246 /* Remove the block from the free-list */
249 pArena
= ARENA_PTR( baseptr
, block
);
250 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
251 LOCAL_RemoveFreeBlock( baseptr
, block
);
253 /* If the previous block is free, expand its size */
255 pTmp
= ARENA_PTR( baseptr
, pArena
->prev
& ~3 );
256 if ((pTmp
->prev
& 3) == LOCAL_ARENA_FREE
)
257 pTmp
->size
+= pArena
->next
- block
;
259 /* Remove the block from the linked list */
261 pTmp
->next
= pArena
->next
;
262 pTmp
= ARENA_PTR( baseptr
, pArena
->next
);
263 pTmp
->prev
= (pTmp
->prev
& 3) | (pArena
->prev
& ~3);
267 /***********************************************************************
270 static void LOCAL_PrintHeap( HANDLE16 ds
)
273 LOCALHEAPINFO
*pInfo
;
276 /* FIXME - the test should be done when calling the function!
277 plus is not clear that we should print this info
278 only when TRACE_ON is on! */
279 if(!TRACE_ON(local
)) return;
281 ptr
= MapSL( MAKESEGPTR( ds
, 0 ));
282 pInfo
= LOCAL_GetHeap( ds
);
286 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds
);
289 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
290 ds
, pInfo
->first
, pInfo
->last
, pInfo
->items
);
292 arena
= pInfo
->first
;
295 LOCALARENA
*pArena
= ARENA_PTR(ptr
,arena
);
296 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena
,
297 pArena
->prev
& ~3, pArena
->next
, pArena
->prev
& 3 );
298 if (arena
== pInfo
->first
)
300 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
301 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
303 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
305 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
306 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
307 if (pArena
->next
== arena
) break; /* last one */
308 if (ARENA_PTR(ptr
,pArena
->free_next
)->free_prev
!= arena
)
310 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
314 if (pArena
->next
== arena
)
316 DPRINTF( "*** last block is not marked free\n" );
319 if ((ARENA_PTR(ptr
,pArena
->next
)->prev
& ~3) != arena
)
321 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
322 pArena
->next
, ARENA_PTR(ptr
,pArena
->next
)->prev
);
325 arena
= pArena
->next
;
330 /***********************************************************************
331 * LocalInit (KERNEL.4)
333 BOOL16 WINAPI
LocalInit16( HANDLE16 selector
, WORD start
, WORD end
)
336 WORD heapInfoArena
, freeArena
, lastArena
;
337 LOCALHEAPINFO
*pHeapInfo
;
338 LOCALARENA
*pArena
, *pFirstArena
, *pLastArena
;
342 /* The initial layout of the heap is: */
343 /* - first arena (FIXED) */
344 /* - heap info structure (FIXED) */
345 /* - large free block (FREE) */
346 /* - last arena (FREE) */
348 TRACE("%04x %04x-%04x\n", selector
, start
, end
);
349 if (!selector
) selector
= CURRENT_DS
;
353 /* If TRACE_ON(heap) is set, the global heap blocks are */
354 /* cleared before use, so we can test for double initialization. */
355 if (LOCAL_GetHeap(selector
))
357 ERR("Heap %04x initialized twice.\n", selector
);
358 LOCAL_PrintHeap(selector
);
364 /* start == 0 means: put the local heap at the end of the segment */
366 DWORD size
= GlobalSize16( GlobalHandle16( selector
) );
367 start
= (WORD
)(size
> 0xffff ? 0xffff : size
) - 1;
368 if ( end
> 0xfffe ) end
= 0xfffe;
374 if ((pModule
= NE_GetPtr( GlobalHandle16( selector
) )))
376 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
);
379 for ( segNr
= 0; segNr
< pModule
->seg_count
; segNr
++, pSeg
++ )
380 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
383 if ( segNr
< pModule
->seg_count
)
385 WORD minsize
= pSeg
->minsize
;
386 if ( pModule
->ss
== segNr
+1 )
387 minsize
+= pModule
->stack_size
;
389 TRACE(" new start %04x, minstart: %04x\n", start
, minsize
);
393 ptr
= MapSL( MAKESEGPTR( selector
, 0 ) );
395 start
= LALIGN( max( start
, sizeof(INSTANCEDATA
) ) );
396 heapInfoArena
= LALIGN(start
+ sizeof(LOCALARENA
) );
397 freeArena
= LALIGN( heapInfoArena
+ ARENA_HEADER_SIZE
398 + sizeof(LOCALHEAPINFO
) );
399 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
401 /* Make sure there's enough space. */
403 if (freeArena
+ sizeof(LOCALARENA
) >= lastArena
) goto done
;
405 /* Initialise the first arena */
407 pFirstArena
= ARENA_PTR( ptr
, start
);
408 pFirstArena
->prev
= start
| LOCAL_ARENA_FIXED
;
409 pFirstArena
->next
= heapInfoArena
;
410 pFirstArena
->size
= LALIGN(sizeof(LOCALARENA
));
411 pFirstArena
->free_prev
= start
; /* this one */
412 pFirstArena
->free_next
= freeArena
;
414 /* Initialise the arena of the heap info structure */
416 pArena
= ARENA_PTR( ptr
, heapInfoArena
);
417 pArena
->prev
= start
| LOCAL_ARENA_FIXED
;
418 pArena
->next
= freeArena
;
420 /* Initialise the heap info structure */
422 pHeapInfo
= (LOCALHEAPINFO
*) (ptr
+ heapInfoArena
+ ARENA_HEADER_SIZE
);
423 memset( pHeapInfo
, 0, sizeof(LOCALHEAPINFO
) );
424 pHeapInfo
->items
= 4;
425 pHeapInfo
->first
= start
;
426 pHeapInfo
->last
= lastArena
;
427 pHeapInfo
->htable
= 0;
428 pHeapInfo
->hdelta
= 0x20;
429 pHeapInfo
->extra
= 0x200;
430 pHeapInfo
->minsize
= lastArena
- freeArena
;
431 pHeapInfo
->magic
= LOCAL_HEAP_MAGIC
;
433 /* Initialise the large free block */
435 pArena
= ARENA_PTR( ptr
, freeArena
);
436 pArena
->prev
= heapInfoArena
| LOCAL_ARENA_FREE
;
437 pArena
->next
= lastArena
;
438 pArena
->size
= lastArena
- freeArena
;
439 pArena
->free_prev
= start
;
440 pArena
->free_next
= lastArena
;
442 /* Initialise the last block */
444 pLastArena
= ARENA_PTR( ptr
, lastArena
);
445 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
446 pLastArena
->next
= lastArena
; /* this one */
447 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
448 pLastArena
->free_prev
= freeArena
;
449 pLastArena
->free_next
= lastArena
; /* this one */
451 /* Store the local heap address in the instance data */
453 ((INSTANCEDATA
*)ptr
)->heap
= heapInfoArena
+ ARENA_HEADER_SIZE
;
454 LOCAL_PrintHeap( selector
);
458 CURRENT_STACK16
->ecx
= ret
; /* must be returned in cx too */
462 /***********************************************************************
465 static BOOL16
LOCAL_GrowHeap( HANDLE16 ds
)
470 LOCALHEAPINFO
*pHeapInfo
;
471 WORD freeArena
, lastArena
;
472 LOCALARENA
*pArena
, *pLastArena
;
475 hseg
= GlobalHandle16( ds
);
476 /* maybe mem allocated by Virtual*() ? */
477 if (!hseg
) return FALSE
;
479 oldsize
= GlobalSize16( hseg
);
480 /* if nothing can be gained, return */
481 if (oldsize
> 0xfff0) return FALSE
;
482 hseg
= GlobalReAlloc16( hseg
, 0x10000, GMEM_FIXED
);
483 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
484 pHeapInfo
= LOCAL_GetHeap( ds
);
485 if (pHeapInfo
== NULL
) {
486 ERR("Heap not found\n" );
489 end
= GlobalSize16( hseg
);
490 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
492 /* Update the HeapInfo */
494 freeArena
= pHeapInfo
->last
;
495 pHeapInfo
->last
= lastArena
;
496 pHeapInfo
->minsize
+= end
- oldsize
;
498 /* grow the old last block */
499 pArena
= ARENA_PTR( ptr
, freeArena
);
500 pArena
->size
= lastArena
- freeArena
;
501 pArena
->next
= lastArena
;
502 pArena
->free_next
= lastArena
;
504 /* Initialise the new last block */
506 pLastArena
= ARENA_PTR( ptr
, lastArena
);
507 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
508 pLastArena
->next
= lastArena
; /* this one */
509 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
510 pLastArena
->free_prev
= freeArena
;
511 pLastArena
->free_next
= lastArena
; /* this one */
513 /* If block before freeArena is also free then merge them */
514 if((ARENA_PTR(ptr
, (pArena
->prev
& ~3))->prev
& 3) == LOCAL_ARENA_FREE
)
516 LOCAL_RemoveBlock(ptr
, freeArena
);
520 TRACE("Heap expanded\n" );
521 LOCAL_PrintHeap( ds
);
526 /***********************************************************************
529 static HLOCAL16
LOCAL_FreeArena( WORD ds
, WORD arena
)
531 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
532 LOCALHEAPINFO
*pInfo
;
533 LOCALARENA
*pArena
, *pPrev
, *pNext
;
535 TRACE("%04x ds=%04x\n", arena
, ds
);
536 if (!(pInfo
= LOCAL_GetHeap( ds
))) return arena
;
538 pArena
= ARENA_PTR( ptr
, arena
);
539 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
541 /* shouldn't happen */
542 ERR("Trying to free block %04x twice!\n",
544 LOCAL_PrintHeap( ds
);
548 /* Check if we can merge with the previous block */
550 pPrev
= ARENA_PTR( ptr
, pArena
->prev
& ~3 );
551 pNext
= ARENA_PTR( ptr
, pArena
->next
);
552 if ((pPrev
->prev
& 3) == LOCAL_ARENA_FREE
)
554 arena
= pArena
->prev
& ~3;
556 LOCAL_RemoveBlock( ptr
, pPrev
->next
);
559 else /* Make a new free block */
561 LOCAL_MakeBlockFree( ptr
, arena
);
564 /* Check if we can merge with the next block */
566 if ((pArena
->next
== pArena
->free_next
) &&
567 (pArena
->next
!= pInfo
->last
))
569 LOCAL_RemoveBlock( ptr
, pArena
->next
);
576 /***********************************************************************
579 * Shrink an arena by creating a free block at its end if possible.
580 * 'size' includes the arena header, and must be aligned.
582 static void LOCAL_ShrinkArena( WORD ds
, WORD arena
, WORD size
)
584 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
585 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
587 if (arena
+ size
+ LALIGN(sizeof(LOCALARENA
)) < pArena
->next
)
589 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
591 LOCAL_AddBlock( ptr
, arena
, arena
+ size
);
593 LOCAL_FreeArena( ds
, arena
+ size
);
598 /***********************************************************************
599 * LOCAL_GrowArenaDownward
601 * Grow an arena downward by using the previous arena (must be free).
603 static void LOCAL_GrowArenaDownward( WORD ds
, WORD arena
, WORD newsize
)
605 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
606 LOCALHEAPINFO
*pInfo
;
607 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
608 WORD prevArena
= pArena
->prev
& ~3;
609 LOCALARENA
*pPrevArena
= ARENA_PTR( ptr
, prevArena
);
613 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
614 offset
= pPrevArena
->size
;
615 size
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
616 LOCAL_RemoveFreeBlock( ptr
, prevArena
);
617 LOCAL_RemoveBlock( ptr
, arena
);
619 p
= (char *)pPrevArena
+ ARENA_HEADER_SIZE
;
620 while (offset
< size
)
622 memcpy( p
, p
+ offset
, offset
);
626 if (size
) memcpy( p
, p
+ offset
, size
);
627 LOCAL_ShrinkArena( ds
, prevArena
, newsize
);
632 /***********************************************************************
633 * LOCAL_GrowArenaUpward
635 * Grow an arena upward by using the next arena (must be free and big
636 * enough). Newsize includes the arena header and must be aligned.
638 static void LOCAL_GrowArenaUpward( WORD ds
, WORD arena
, WORD newsize
)
640 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
641 LOCALHEAPINFO
*pInfo
;
642 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
643 WORD nextArena
= pArena
->next
;
645 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
646 LOCAL_RemoveBlock( ptr
, nextArena
);
648 LOCAL_ShrinkArena( ds
, arena
, newsize
);
652 /***********************************************************************
655 static WORD
LOCAL_GetFreeSpace(WORD ds
, WORD countdiscard
)
657 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
658 LOCALHEAPINFO
*pInfo
;
663 if (!(pInfo
= LOCAL_GetHeap( ds
)))
665 ERR("Local heap not found\n" );
669 arena
= pInfo
->first
;
670 pArena
= ARENA_PTR( ptr
, arena
);
671 while (arena
!= pArena
->free_next
)
673 arena
= pArena
->free_next
;
674 pArena
= ARENA_PTR( ptr
, arena
);
675 if (pArena
->size
>= freespace
) freespace
= pArena
->size
;
677 /* FIXME doesn't yet calculate space that would become free if everything
678 were discarded when countdiscard == 1 */
679 if (freespace
< ARENA_HEADER_SIZE
) freespace
= 0;
680 else freespace
-= ARENA_HEADER_SIZE
;
685 /***********************************************************************
688 WORD
LOCAL_Compact( HANDLE16 ds
, UINT16 minfree
, UINT16 flags
)
690 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
691 LOCALHEAPINFO
*pInfo
;
692 LOCALARENA
*pArena
, *pMoveArena
, *pFinalArena
;
693 WORD arena
, movearena
, finalarena
, table
;
694 WORD count
, movesize
, size
;
696 LOCALHANDLEENTRY
*pEntry
;
698 if (!(pInfo
= LOCAL_GetHeap( ds
)))
700 ERR("Local heap not found\n" );
704 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
706 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
707 if(freespace
>= minfree
|| (flags
& LMEM_NOCOMPACT
))
709 TRACE("Returning %04x.\n", freespace
);
712 TRACE("Compacting heap %04x.\n", ds
);
713 table
= pInfo
->htable
;
716 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
717 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
719 if((pEntry
->lock
== 0) && (pEntry
->flags
!= (LMEM_DISCARDED
>> 8)))
721 /* OK we can move this one if we want */
722 TRACE("handle %04x (block %04x) can be moved.\n",
723 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
724 movearena
= ARENA_HEADER(pEntry
->addr
);
725 pMoveArena
= ARENA_PTR(ptr
, movearena
);
726 movesize
= pMoveArena
->next
- movearena
;
727 arena
= pInfo
->first
;
728 pArena
= ARENA_PTR(ptr
, arena
);
731 /* Try to find the smallest arena that will do, */
732 /* which is below us in memory */
735 arena
= pArena
->free_next
;
736 pArena
= ARENA_PTR(ptr
, arena
);
737 if(arena
>= movearena
)
739 if(arena
== pArena
->free_next
)
741 if((pArena
->size
>= movesize
) && (pArena
->size
< size
))
747 if (finalarena
) /* Actually got somewhere to move */
749 TRACE("Moving it to %04x.\n", finalarena
);
750 pFinalArena
= ARENA_PTR(ptr
, finalarena
);
751 size
= pFinalArena
->size
;
752 LOCAL_RemoveFreeBlock(ptr
, finalarena
);
753 LOCAL_ShrinkArena( ds
, finalarena
, movesize
);
754 /* Copy the arena to it's new location */
755 memcpy((char *)pFinalArena
+ ARENA_HEADER_SIZE
,
756 (char *)pMoveArena
+ ARENA_HEADER_SIZE
,
757 movesize
- ARENA_HEADER_SIZE
);
758 /* Free the old location */
759 LOCAL_FreeArena(ds
, movearena
);
761 LOCAL_CallTo16_word_www(pInfo
->notify
, LN_MOVE
,
762 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
763 /* Update handle table entry */
764 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ sizeof(HLOCAL16
) ;
766 else if((ARENA_PTR(ptr
, pMoveArena
->prev
& ~3)->prev
& 3)
769 /* Previous arena is free (but < movesize) */
770 /* so we can 'slide' movearena down into it */
771 finalarena
= pMoveArena
->prev
& ~3;
772 LOCAL_GrowArenaDownward( ds
, movearena
, movesize
);
773 /* Update handle table entry */
774 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ sizeof(HLOCAL16
) ;
778 table
= *(WORD
*)pEntry
;
780 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
781 if(freespace
>= minfree
|| (flags
& LMEM_NODISCARD
))
783 TRACE("Returning %04x.\n", freespace
);
787 table
= pInfo
->htable
;
790 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
791 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
793 if(pEntry
->addr
&& pEntry
->lock
== 0 &&
794 (pEntry
->flags
& (LMEM_DISCARDABLE
>> 8)))
796 TRACE("Discarding handle %04x (block %04x).\n",
797 (char *)pEntry
- ptr
, pEntry
->addr
);
798 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
));
800 LOCAL_CallTo16_word_www(pInfo
->notify
, LN_DISCARD
,
801 (char *)pEntry
- ptr
, pEntry
->flags
);
803 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
806 table
= *(WORD
*)pEntry
;
808 return LOCAL_Compact(ds
, 0xffff, LMEM_NODISCARD
);
812 /***********************************************************************
813 * LOCAL_FindFreeBlock
815 static HLOCAL16
LOCAL_FindFreeBlock( HANDLE16 ds
, WORD size
)
817 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
818 LOCALHEAPINFO
*pInfo
;
822 if (!(pInfo
= LOCAL_GetHeap( ds
)))
824 ERR("Local heap not found\n" );
829 arena
= pInfo
->first
;
830 pArena
= ARENA_PTR( ptr
, arena
);
832 arena
= pArena
->free_next
;
833 pArena
= ARENA_PTR( ptr
, arena
);
834 if (arena
== pArena
->free_next
) break;
835 if (pArena
->size
>= size
) return arena
;
837 TRACE("not enough space\n" );
843 /***********************************************************************
846 static const char *get_heap_name( WORD ds
)
848 HINSTANCE16 inst
= LoadLibrary16( "GDI" );
849 if (ds
== GlobalHandleToSel16( inst
))
851 FreeLibrary16( inst
);
854 FreeLibrary16( inst
);
855 inst
= LoadLibrary16( "USER" );
856 if (ds
== GlobalHandleToSel16( inst
))
858 FreeLibrary16( inst
);
861 FreeLibrary16( inst
);
865 /***********************************************************************
867 * The segment may get moved around in this function, so all callers
868 * should reset their pointer variables.
870 static HLOCAL16
LOCAL_GetBlock( HANDLE16 ds
, WORD size
, WORD flags
)
872 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
873 LOCALHEAPINFO
*pInfo
;
877 if (!(pInfo
= LOCAL_GetHeap( ds
)))
879 ERR("Local heap not found\n");
884 size
+= ARENA_HEADER_SIZE
;
885 size
= LALIGN( max( size
, sizeof(LOCALARENA
) ) );
890 /* Find a suitable free block */
891 arena
= LOCAL_FindFreeBlock( ds
, size
);
893 /* no space: try to make some */
894 LOCAL_Compact( ds
, size
, flags
);
895 arena
= LOCAL_FindFreeBlock( ds
, size
);
898 /* still no space: try to grow the segment */
899 if (!(LOCAL_GrowHeap( ds
)))
902 /* FIXME: doesn't work correctly yet */
903 if ((pInfo
->notify
) && (LOCAL_CallTo16_word_www(pInfo
->notify
, LN_OUTOFMEM
, ds
- 20, size
))) /* FIXME: "size" correct ? (should indicate bytes needed) */
908 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
909 pInfo
= LOCAL_GetHeap( ds
);
910 arena
= LOCAL_FindFreeBlock( ds
, size
);
913 ERR( "not enough space in %s heap %04x for %d bytes\n",
914 get_heap_name(ds
), ds
, size
);
916 if ((pInfo
->notify
) &&
917 /* FIXME: "size" correct ? (should indicate bytes needed) */
918 (LOCAL_CallTo16_word_www(pInfo
->notify
, LN_OUTOFMEM
, ds
, size
)))
924 /* Make a block out of the free arena */
925 pArena
= ARENA_PTR( ptr
, arena
);
926 TRACE("size = %04x, arena %04x size %04x\n",
927 size
, arena
, pArena
->size
);
928 LOCAL_RemoveFreeBlock( ptr
, arena
);
929 LOCAL_ShrinkArena( ds
, arena
, size
);
931 if (flags
& LMEM_ZEROINIT
)
932 memset((char *)pArena
+ ARENA_HEADER_SIZE
, 0, size
-ARENA_HEADER_SIZE
);
933 return arena
+ ARENA_HEADER_SIZE
;
937 /***********************************************************************
940 static BOOL16
LOCAL_NewHTable( HANDLE16 ds
)
942 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
943 LOCALHEAPINFO
*pInfo
;
944 LOCALHANDLEENTRY
*pEntry
;
949 if (!(pInfo
= LOCAL_GetHeap( ds
)))
951 ERR("Local heap not found\n");
956 if (!(handle
= LOCAL_GetBlock( ds
, pInfo
->hdelta
* sizeof(LOCALHANDLEENTRY
)
957 + 2 * sizeof(WORD
), LMEM_FIXED
)))
959 if (!(ptr
= MapSL( MAKESEGPTR( ds
, 0 ) )))
960 ERR("ptr == NULL after GetBlock.\n");
961 if (!(pInfo
= LOCAL_GetHeap( ds
)))
962 ERR("pInfo == NULL after GetBlock.\n");
964 /* Fill the entry table */
966 *(WORD
*)(ptr
+ handle
) = pInfo
->hdelta
;
967 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
+ sizeof(WORD
));
968 for (i
= pInfo
->hdelta
; i
> 0; i
--, pEntry
++) {
969 pEntry
->lock
= pEntry
->flags
= 0xff;
972 *(WORD
*)pEntry
= pInfo
->htable
;
973 pInfo
->htable
= handle
;
978 /***********************************************************************
979 * LOCAL_GetNewHandleEntry
981 static HLOCAL16
LOCAL_GetNewHandleEntry( HANDLE16 ds
)
983 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
984 LOCALHEAPINFO
*pInfo
;
985 LOCALHANDLEENTRY
*pEntry
= NULL
;
988 if (!(pInfo
= LOCAL_GetHeap( ds
)))
990 ERR("Local heap not found\n");
995 /* Find a free slot in existing tables */
997 table
= pInfo
->htable
;
1000 WORD count
= *(WORD
*)(ptr
+ table
);
1001 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1002 for (; count
> 0; count
--, pEntry
++)
1003 if (pEntry
->lock
== 0xff) break;
1005 table
= *(WORD
*)pEntry
;
1008 if (!table
) /* We need to create a new table */
1010 if (!LOCAL_NewHTable( ds
)) return 0;
1011 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1012 pInfo
= LOCAL_GetHeap( ds
);
1013 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ pInfo
->htable
+ sizeof(WORD
));
1016 /* Now allocate this entry */
1020 TRACE("(%04x): %04x\n",
1021 ds
, ((char *)pEntry
- ptr
) );
1022 return (HLOCAL16
)((char *)pEntry
- ptr
);
1026 /***********************************************************************
1027 * LOCAL_FreeHandleEntry
1029 * Free a handle table entry.
1031 static void LOCAL_FreeHandleEntry( HANDLE16 ds
, HLOCAL16 handle
)
1033 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1034 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1035 LOCALHEAPINFO
*pInfo
;
1037 WORD table
, count
, i
;
1039 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
1041 /* Find the table where this handle comes from */
1043 pTable
= &pInfo
->htable
;
1046 WORD size
= (*(WORD
*)(ptr
+ *pTable
)) * sizeof(LOCALHANDLEENTRY
);
1047 if ((handle
>= *pTable
+ sizeof(WORD
)) &&
1048 (handle
< *pTable
+ sizeof(WORD
) + size
)) break; /* Found it */
1049 pTable
= (WORD
*)(ptr
+ *pTable
+ sizeof(WORD
) + size
);
1053 ERR("Invalid entry %04x\n", handle
);
1054 LOCAL_PrintHeap( ds
);
1058 /* Make the entry free */
1060 pEntry
->addr
= 0; /* just in case */
1061 pEntry
->lock
= 0xff;
1062 pEntry
->flags
= 0xff;
1063 /* Now check if all entries in this table are free */
1066 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1067 count
= *(WORD
*)(ptr
+ table
);
1068 for (i
= count
; i
> 0; i
--, pEntry
++) if (pEntry
->lock
!= 0xff) return;
1070 /* Remove the table from the linked list and free it */
1072 TRACE("(%04x): freeing table %04x\n",
1074 *pTable
= *(WORD
*)pEntry
;
1075 LOCAL_FreeArena( ds
, ARENA_HEADER( table
) );
1079 /***********************************************************************
1082 * Implementation of LocalFree().
1084 HLOCAL16
LOCAL_Free( HANDLE16 ds
, HLOCAL16 handle
)
1086 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1088 TRACE("%04x ds=%04x\n", handle
, ds
);
1090 if (!handle
) { WARN("Handle is 0.\n" ); return 0; }
1091 if (HANDLE_FIXED( handle
))
1093 if (!LOCAL_FreeArena( ds
, ARENA_HEADER( handle
) )) return 0; /* OK */
1094 else return handle
; /* couldn't free it */
1098 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1099 if (pEntry
->flags
!= (LMEM_DISCARDED
>> 8))
1101 TRACE("real block at %04x\n",
1103 if (LOCAL_FreeArena( ds
, ARENA_HEADER(pEntry
->addr
) ))
1104 return handle
; /* couldn't free it */
1106 LOCAL_FreeHandleEntry( ds
, handle
);
1112 /***********************************************************************
1115 * Implementation of LocalAlloc().
1118 HLOCAL16
LOCAL_Alloc( HANDLE16 ds
, WORD flags
, WORD size
)
1123 TRACE("%04x %d ds=%04x\n", flags
, size
, ds
);
1125 if(size
> 0 && size
<= 4) size
= 5;
1126 if (flags
& LMEM_MOVEABLE
)
1128 LOCALHANDLEENTRY
*plhe
;
1133 if (!(hmem
= LOCAL_GetBlock( ds
, size
+ sizeof(HLOCAL16
), flags
)))
1136 else /* We just need to allocate a discarded handle */
1138 if (!(handle
= LOCAL_GetNewHandleEntry( ds
)))
1140 WARN("Couldn't get handle.\n");
1142 LOCAL_FreeArena( ds
, ARENA_HEADER(hmem
) );
1145 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1146 plhe
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1150 plhe
->addr
= hmem
+ sizeof(HLOCAL16
);
1151 plhe
->flags
= (BYTE
)((flags
& 0x0f00) >> 8);
1152 *(HLOCAL16
*)(ptr
+ hmem
) = handle
;
1157 plhe
->flags
= LMEM_DISCARDED
>> 8;
1164 handle
= LOCAL_GetBlock( ds
, size
, flags
);
1170 /***********************************************************************
1173 * Implementation of LocalReAlloc().
1175 HLOCAL16
LOCAL_ReAlloc( HANDLE16 ds
, HLOCAL16 handle
, WORD size
, WORD flags
)
1177 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1178 LOCALHEAPINFO
*pInfo
;
1179 LOCALARENA
*pArena
, *pNext
;
1180 LOCALHANDLEENTRY
*pEntry
= NULL
;
1181 WORD arena
, oldsize
;
1182 HLOCAL16 hmem
, blockhandle
;
1185 if (!handle
) return 0;
1186 if(HANDLE_MOVEABLE(handle
) &&
1187 ((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
== 0xff) /* An unused handle */
1190 TRACE("%04x %d %04x ds=%04x\n",
1191 handle
, size
, flags
, ds
);
1192 if (!(pInfo
= LOCAL_GetHeap( ds
))) return 0;
1194 if (HANDLE_FIXED( handle
))
1195 blockhandle
= handle
;
1198 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1199 if(pEntry
->flags
== (LMEM_DISCARDED
>> 8))
1203 WARN("Dicarded block has non-zero addr.\n");
1204 TRACE("ReAllocating discarded block\n");
1205 if(size
<= 4) size
= 5;
1206 if (!(hl
= LOCAL_GetBlock( ds
, size
+ sizeof(HLOCAL16
), flags
)))
1208 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) ); /* Reload ptr */
1209 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1210 pEntry
->addr
= hl
+ sizeof(HLOCAL16
);
1213 *(HLOCAL16
*)(ptr
+ hl
) = handle
;
1216 if (((blockhandle
= pEntry
->addr
) & 3) != 2)
1218 ERR("(%04x,%04x): invalid handle\n",
1222 if(*((HLOCAL16
*)(ptr
+ blockhandle
) - 1) != handle
) {
1223 ERR("Back ptr to handle is invalid\n");
1228 if (flags
& LMEM_MODIFY
)
1230 if (HANDLE_MOVEABLE(handle
))
1232 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1233 pEntry
->flags
= (flags
& 0x0f00) >> 8;
1234 TRACE("Changing flags to %x.\n", pEntry
->flags
);
1241 if (flags
& LMEM_MOVEABLE
)
1243 if (HANDLE_FIXED(handle
))
1245 TRACE("Freeing fixed block.\n");
1246 return LOCAL_Free( ds
, handle
);
1248 else /* Moveable block */
1250 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1251 if (pEntry
->lock
== 0)
1253 /* discards moveable blocks */
1254 TRACE("Discarding block\n");
1255 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
));
1257 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
1265 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1266 if (pEntry
->lock
== 0)
1269 return LOCAL_Free( ds
, handle
);
1275 arena
= ARENA_HEADER( blockhandle
);
1276 TRACE("arena is %04x\n", arena
);
1277 pArena
= ARENA_PTR( ptr
, arena
);
1279 if(size
<= 4) size
= 5;
1280 oldsize
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
1281 nextarena
= LALIGN(blockhandle
+ size
);
1283 /* Check for size reduction */
1285 if (nextarena
<= pArena
->next
)
1287 TRACE("size reduction, making new free block\n");
1288 LOCAL_ShrinkArena(ds
, arena
, nextarena
- arena
);
1289 TRACE("returning %04x\n", handle
);
1293 /* Check if the next block is free and large enough */
1295 pNext
= ARENA_PTR( ptr
, pArena
->next
);
1296 if (((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) &&
1297 (nextarena
<= pNext
->next
))
1299 TRACE("size increase, making new free block\n");
1300 LOCAL_GrowArenaUpward(ds
, arena
, nextarena
- arena
);
1301 TRACE("returning %04x\n", handle
);
1305 /* Now we have to allocate a new block, but not if (fixed block or locked
1306 block) and no LMEM_MOVEABLE */
1308 if (!(flags
& LMEM_MOVEABLE
))
1310 if (HANDLE_FIXED(handle
))
1312 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1317 if(((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
!= 0)
1319 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1324 if(HANDLE_MOVEABLE(handle
)) size
+= sizeof(HLOCAL16
);
1325 hmem
= LOCAL_GetBlock( ds
, size
, flags
);
1326 ptr
= MapSL( MAKESEGPTR( ds
, 0 )); /* Reload ptr */
1327 if(HANDLE_MOVEABLE(handle
)) /* LOCAL_GetBlock might have triggered */
1328 { /* a compaction, which might in turn have */
1329 blockhandle
= pEntry
->addr
; /* moved the very block we are resizing */
1330 arena
= ARENA_HEADER( blockhandle
); /* thus, we reload arena, too */
1334 /* Remove the block from the heap and try again */
1335 LPSTR buffer
= HeapAlloc( GetProcessHeap(), 0, oldsize
);
1336 if (!buffer
) return 0;
1337 memcpy( buffer
, ptr
+ arena
+ ARENA_HEADER_SIZE
, oldsize
);
1338 LOCAL_FreeArena( ds
, arena
);
1339 if (!(hmem
= LOCAL_GetBlock( ds
, size
, flags
)))
1341 if (!(hmem
= LOCAL_GetBlock( ds
, oldsize
, flags
)))
1343 ERR("Can't restore saved block\n" );
1344 HeapFree( GetProcessHeap(), 0, buffer
);
1349 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) ); /* Reload ptr */
1350 memcpy( ptr
+ hmem
, buffer
, oldsize
);
1351 HeapFree( GetProcessHeap(), 0, buffer
);
1355 memcpy( ptr
+ hmem
, ptr
+ (arena
+ ARENA_HEADER_SIZE
), oldsize
);
1356 LOCAL_FreeArena( ds
, arena
);
1358 if (HANDLE_MOVEABLE( handle
))
1360 TRACE("fixing handle\n");
1361 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1362 pEntry
->addr
= hmem
+ sizeof(HLOCAL16
);
1363 /* Back ptr should still be correct */
1364 if(*(HLOCAL16
*)(ptr
+ hmem
) != handle
)
1365 ERR("back ptr is invalid.\n");
1368 if (size
== oldsize
) hmem
= 0; /* Realloc failed */
1369 TRACE("returning %04x\n", hmem
);
1374 /***********************************************************************
1375 * LOCAL_InternalLock
1377 static HLOCAL16
LOCAL_InternalLock( LPSTR heap
, HLOCAL16 handle
)
1379 HLOCAL16 old_handle
= handle
;
1381 if (HANDLE_MOVEABLE(handle
))
1383 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(heap
+ handle
);
1384 if (pEntry
->flags
== LMEM_DISCARDED
) return 0;
1385 if (pEntry
->lock
< 0xfe) pEntry
->lock
++;
1386 handle
= pEntry
->addr
;
1388 TRACE("%04x returning %04x\n",
1389 old_handle
, handle
);
1394 /***********************************************************************
1397 void *LOCAL_Lock( HANDLE16 ds
, HLOCAL16 handle
)
1399 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1400 return handle
? ptr
+ LOCAL_InternalLock( ptr
, handle
) : NULL
;
1404 /***********************************************************************
1407 BOOL16
LOCAL_Unlock( HANDLE16 ds
, HLOCAL16 handle
)
1409 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1411 TRACE("%04x\n", handle
);
1412 if (HANDLE_MOVEABLE(handle
))
1414 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1415 if (!pEntry
->lock
|| (pEntry
->lock
== 0xff)) return FALSE
;
1416 /* For moveable block, return the new lock count */
1417 /* (see _Windows_Internals_ p. 197) */
1418 return --pEntry
->lock
;
1424 /***********************************************************************
1427 * Implementation of LocalSize().
1429 WORD
LOCAL_Size( HANDLE16 ds
, HLOCAL16 handle
)
1431 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1434 TRACE("%04x ds=%04x\n", handle
, ds
);
1436 if (HANDLE_MOVEABLE( handle
)) handle
= *(WORD
*)(ptr
+ handle
);
1437 if (!handle
) return 0;
1438 pArena
= ARENA_PTR( ptr
, ARENA_HEADER(handle
) );
1439 return pArena
->next
- handle
;
1443 /***********************************************************************
1446 * Implementation of LocalFlags().
1448 WORD
LOCAL_Flags( HANDLE16 ds
, HLOCAL16 handle
)
1450 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1452 if (HANDLE_MOVEABLE(handle
))
1454 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1455 TRACE("(%04x,%04x): returning %04x\n",
1456 ds
, handle
, pEntry
->lock
| (pEntry
->flags
<< 8) );
1457 return pEntry
->lock
| (pEntry
->flags
<< 8);
1461 TRACE("(%04x,%04x): returning 0\n",
1468 /***********************************************************************
1471 * Implementation of LocalHeapSize().
1473 WORD
LOCAL_HeapSize( HANDLE16 ds
)
1475 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1476 if (!pInfo
) return 0;
1477 return pInfo
->last
- pInfo
->first
;
1481 /***********************************************************************
1484 * Implementation of LocalCountFree().
1486 WORD
LOCAL_CountFree( HANDLE16 ds
)
1490 LOCALHEAPINFO
*pInfo
;
1491 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1493 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1495 ERR("(%04x): Local heap not found\n", ds
);
1496 LOCAL_PrintHeap( ds
);
1501 arena
= pInfo
->first
;
1502 pArena
= ARENA_PTR( ptr
, arena
);
1505 arena
= pArena
->free_next
;
1506 pArena
= ARENA_PTR( ptr
, arena
);
1507 if (arena
== pArena
->free_next
) break;
1508 total
+= pArena
->size
;
1510 TRACE("(%04x): returning %d\n", ds
, total
);
1515 /***********************************************************************
1518 * Implementation of LocalHandle().
1520 HLOCAL16
LOCAL_Handle( HANDLE16 ds
, WORD addr
)
1522 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1523 LOCALHEAPINFO
*pInfo
;
1526 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1528 ERR("(%04x): Local heap not found\n", ds
);
1529 LOCAL_PrintHeap( ds
);
1533 /* Find the address in the entry tables */
1535 table
= pInfo
->htable
;
1538 WORD count
= *(WORD
*)(ptr
+ table
);
1539 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+table
+sizeof(WORD
));
1540 for (; count
> 0; count
--, pEntry
++)
1541 if (pEntry
->addr
== addr
) return (HLOCAL16
)((char *)pEntry
- ptr
);
1542 table
= *(WORD
*)pEntry
;
1545 return (HLOCAL16
)addr
; /* Fixed block handle is addr */
1549 /***********************************************************************
1550 * LocalAlloc16 (KERNEL.5)
1552 HLOCAL16 WINAPI
LocalAlloc16( UINT16 flags
, WORD size
)
1554 HLOCAL16 ret
= LOCAL_Alloc( CURRENT_DS
, flags
, size
);
1555 CURRENT_STACK16
->ecx
= ret
; /* must be returned in cx too */
1560 /***********************************************************************
1561 * LocalReAlloc16 (KERNEL.6)
1563 HLOCAL16 WINAPI
LocalReAlloc16( HLOCAL16 handle
, WORD size
, UINT16 flags
)
1565 return LOCAL_ReAlloc( CURRENT_DS
, handle
, size
, flags
);
1569 /***********************************************************************
1570 * LocalFree16 (KERNEL.7)
1572 HLOCAL16 WINAPI
LocalFree16( HLOCAL16 handle
)
1574 return LOCAL_Free( CURRENT_DS
, handle
);
1578 /***********************************************************************
1579 * LocalLock16 (KERNEL.8)
1581 * Note: only the offset part of the pointer is returned by the relay code.
1583 SEGPTR WINAPI
LocalLock16( HLOCAL16 handle
)
1585 WORD ds
= CURRENT_DS
;
1586 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1587 return MAKESEGPTR( ds
, LOCAL_InternalLock( ptr
, handle
) );
1591 /***********************************************************************
1592 * LocalUnlock16 (KERNEL.9)
1594 BOOL16 WINAPI
LocalUnlock16( HLOCAL16 handle
)
1596 return LOCAL_Unlock( CURRENT_DS
, handle
);
1600 /***********************************************************************
1601 * LocalSize16 (KERNEL.10)
1603 UINT16 WINAPI
LocalSize16( HLOCAL16 handle
)
1605 return LOCAL_Size( CURRENT_DS
, handle
);
1609 /***********************************************************************
1610 * LocalHandle16 (KERNEL.11)
1612 HLOCAL16 WINAPI
LocalHandle16( WORD addr
)
1614 return LOCAL_Handle( CURRENT_DS
, addr
);
1618 /***********************************************************************
1619 * LocalFlags16 (KERNEL.12)
1621 UINT16 WINAPI
LocalFlags16( HLOCAL16 handle
)
1623 return LOCAL_Flags( CURRENT_DS
, handle
);
1627 /***********************************************************************
1628 * LocalCompact16 (KERNEL.13)
1630 UINT16 WINAPI
LocalCompact16( UINT16 minfree
)
1632 TRACE("%04x\n", minfree
);
1633 return LOCAL_Compact( CURRENT_DS
, minfree
, 0 );
1637 /***********************************************************************
1638 * LocalNotify (KERNEL.14)
1640 * Installs a callback function that is called for local memory events
1641 * Callback function prototype is
1642 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1645 * NotifyFunc seems to be responsible for allocating some memory,
1646 * returns TRUE for success.
1647 * wArg = number of bytes needed additionally
1649 * hMem = handle; wArg = old mem location
1651 * NotifyFunc seems to be strongly encouraged to return TRUE,
1652 * otherwise LogError() gets called.
1653 * hMem = handle; wArg = flags
1655 FARPROC16 WINAPI
LocalNotify16( FARPROC16 func
)
1657 LOCALHEAPINFO
*pInfo
;
1658 FARPROC16 oldNotify
;
1659 HANDLE16 ds
= CURRENT_DS
;
1661 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1663 ERR("(%04x): Local heap not found\n", ds
);
1664 LOCAL_PrintHeap( ds
);
1667 TRACE("(%04x): %08lx\n", ds
, (DWORD
)func
);
1668 FIXME("Half implemented\n");
1669 oldNotify
= pInfo
->notify
;
1670 pInfo
->notify
= func
;
1675 /***********************************************************************
1676 * LocalShrink16 (KERNEL.121)
1678 UINT16 WINAPI
LocalShrink16( HGLOBAL16 handle
, UINT16 newsize
)
1680 TRACE("%04x %04x\n", handle
, newsize
);
1685 /***********************************************************************
1686 * GetHeapSpaces (KERNEL.138)
1688 DWORD WINAPI
GetHeapSpaces16( HMODULE16 module
)
1693 if (!(pModule
= NE_GetPtr( module
))) return 0;
1695 GlobalHandleToSel16((NE_SEG_TABLE( pModule
) + pModule
->dgroup
- 1)->hSeg
);
1696 return MAKELONG( LOCAL_CountFree( ds
), LOCAL_HeapSize( ds
) );
1700 /***********************************************************************
1701 * LocalCountFree (KERNEL.161)
1703 WORD WINAPI
LocalCountFree16(void)
1705 return LOCAL_CountFree( CURRENT_DS
);
1709 /***********************************************************************
1710 * LocalHeapSize (KERNEL.162)
1712 WORD WINAPI
LocalHeapSize16(void)
1715 return LOCAL_HeapSize( CURRENT_DS
);
1719 /***********************************************************************
1720 * LocalHandleDelta (KERNEL.310)
1722 WORD WINAPI
LocalHandleDelta16( WORD delta
)
1724 LOCALHEAPINFO
*pInfo
;
1726 if (!(pInfo
= LOCAL_GetHeap( CURRENT_DS
)))
1728 ERR("Local heap not found\n");
1729 LOCAL_PrintHeap( CURRENT_DS
);
1732 if (delta
) pInfo
->hdelta
= delta
;
1733 TRACE("returning %04x\n", pInfo
->hdelta
);
1734 return pInfo
->hdelta
;
1738 /***********************************************************************
1739 * LocalInfo (TOOLHELP.56)
1741 BOOL16 WINAPI
LocalInfo16( LOCALINFO
*pLocalInfo
, HGLOBAL16 handle
)
1743 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle
)));
1744 if (!pInfo
) return FALSE
;
1745 pLocalInfo
->wcItems
= pInfo
->items
;
1750 /***********************************************************************
1751 * LocalFirst (TOOLHELP.57)
1753 BOOL16 WINAPI
LocalFirst16( LOCALENTRY
*pLocalEntry
, HGLOBAL16 handle
)
1755 WORD ds
= GlobalHandleToSel16( handle
);
1756 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1757 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1758 if (!pInfo
) return FALSE
;
1760 pLocalEntry
->hHandle
= pInfo
->first
+ ARENA_HEADER_SIZE
;
1761 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1762 pLocalEntry
->wFlags
= LF_FIXED
;
1763 pLocalEntry
->wcLock
= 0;
1764 pLocalEntry
->wType
= LT_NORMAL
;
1765 pLocalEntry
->hHeap
= handle
;
1766 pLocalEntry
->wHeapType
= NORMAL_HEAP
;
1767 pLocalEntry
->wNext
= ARENA_PTR(ptr
,pInfo
->first
)->next
;
1768 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1773 /***********************************************************************
1774 * LocalNext (TOOLHELP.58)
1776 BOOL16 WINAPI
LocalNext16( LOCALENTRY
*pLocalEntry
)
1778 WORD ds
= GlobalHandleToSel16( pLocalEntry
->hHeap
);
1779 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1782 if (!LOCAL_GetHeap( ds
)) return FALSE
;
1783 if (!pLocalEntry
->wNext
) return FALSE
;
1784 pArena
= ARENA_PTR( ptr
, pLocalEntry
->wNext
);
1786 pLocalEntry
->hHandle
= pLocalEntry
->wNext
+ ARENA_HEADER_SIZE
;
1787 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1788 pLocalEntry
->wFlags
= (pArena
->prev
& 3) + 1;
1789 pLocalEntry
->wcLock
= 0;
1790 pLocalEntry
->wType
= LT_NORMAL
;
1791 if (pArena
->next
!= pLocalEntry
->wNext
) /* last one? */
1792 pLocalEntry
->wNext
= pArena
->next
;
1794 pLocalEntry
->wNext
= 0;
1795 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1800 /***********************************************************************
1801 * LocalAlloc (KERNEL32.371)
1806 HLOCAL WINAPI
LocalAlloc(
1807 UINT flags
, /* [in] Allocation attributes */
1808 DWORD size
/* [in] Number of bytes to allocate */
1810 return (HLOCAL
)GlobalAlloc( flags
, size
);
1814 /***********************************************************************
1815 * LocalCompact (KERNEL32.372)
1817 UINT WINAPI
LocalCompact( UINT minfree
)
1819 return 0; /* LocalCompact does nothing in Win32 */
1823 /***********************************************************************
1824 * LocalFlags (KERNEL32.374)
1826 * Value specifying allocation flags and lock count.
1827 * LMEM_INVALID_HANDLE: Failure
1829 UINT WINAPI
LocalFlags(
1830 HLOCAL handle
/* [in] Handle of memory object */
1832 return GlobalFlags( (HGLOBAL
)handle
);
1836 /***********************************************************************
1837 * LocalFree (KERNEL32.375)
1842 HLOCAL WINAPI
LocalFree(
1843 HLOCAL handle
/* [in] Handle of memory object */
1845 return (HLOCAL
)GlobalFree( (HGLOBAL
)handle
);
1849 /***********************************************************************
1850 * LocalHandle (KERNEL32.376)
1855 HLOCAL WINAPI
LocalHandle(
1856 LPCVOID ptr
/* [in] Address of local memory object */
1858 return (HLOCAL
)GlobalHandle( ptr
);
1862 /***********************************************************************
1863 * LocalLock (KERNEL32.377)
1864 * Locks a local memory object and returns pointer to the first byte
1865 * of the memory block.
1871 LPVOID WINAPI
LocalLock(
1872 HLOCAL handle
/* [in] Address of local memory object */
1874 return GlobalLock( (HGLOBAL
)handle
);
1878 /***********************************************************************
1879 * LocalReAlloc (KERNEL32.378)
1884 HLOCAL WINAPI
LocalReAlloc(
1885 HLOCAL handle
, /* [in] Handle of memory object */
1886 DWORD size
, /* [in] New size of block */
1887 UINT flags
/* [in] How to reallocate object */
1889 return (HLOCAL
)GlobalReAlloc( (HGLOBAL
)handle
, size
, flags
);
1893 /***********************************************************************
1894 * LocalShrink (KERNEL32.379)
1896 UINT WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
1898 return 0; /* LocalShrink does nothing in Win32 */
1902 /***********************************************************************
1903 * LocalSize (KERNEL32.380)
1908 UINT WINAPI
LocalSize(
1909 HLOCAL handle
/* [in] Handle of memory object */
1911 return GlobalSize( (HGLOBAL
)handle
);
1915 /***********************************************************************
1916 * LocalUnlock (KERNEL32.381)
1918 * TRUE: Object is still locked
1919 * FALSE: Object is unlocked
1921 BOOL WINAPI
LocalUnlock(
1922 HLOCAL handle
/* [in] Handle of memory object */
1924 return GlobalUnlock( (HGLOBAL
)handle
);