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"
25 #include "stackframe.h"
27 #include "debugtools.h"
30 DECLARE_DEBUG_CHANNEL(heap
)
31 DECLARE_DEBUG_CHANNEL(local
)
36 WORD prev
; /* Previous arena | arena type */
37 WORD next
; /* Next arena */
38 /* Start of the memory block or free-list info */
39 WORD size
; /* Size of the free block */
40 WORD free_prev
; /* Previous free block */
41 WORD free_next
; /* Next free block */
44 #define ARENA_HEADER_SIZE 4
45 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
47 /* Arena types (stored in 'prev' field of the arena) */
48 #define LOCAL_ARENA_FREE 0
49 #define LOCAL_ARENA_FIXED 1
51 /* LocalNotify() msgs */
57 /* Layout of a handle entry table
59 * WORD count of entries
60 * LOCALHANDLEENTRY[count] entries
61 * WORD near ptr to next table
65 WORD addr
; /* Address of the MOVEABLE block */
66 BYTE flags
; /* Flags for this block */
67 BYTE lock
; /* Lock count */
71 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
72 * in case something actually relies on this.
73 * Note the ARENA_HEADER(addr) still produces the desired result ie. 4n - 4
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)
86 WORD check
; /* 00 Heap checking flag */
87 WORD freeze
; /* 02 Heap frozen flag */
88 WORD items
; /* 04 Count of items on the heap */
89 WORD first
; /* 06 First item of the heap */
90 WORD pad1
; /* 08 Always 0 */
91 WORD last
; /* 0a Last item of the heap */
92 WORD pad2
; /* 0c Always 0 */
93 BYTE ncompact
; /* 0e Compactions counter */
94 BYTE dislevel
; /* 0f Discard level */
95 DWORD distotal
; /* 10 Total bytes discarded */
96 WORD htable
; /* 14 Pointer to handle table */
97 WORD hfree
; /* 16 Pointer to free handle table */
98 WORD hdelta
; /* 18 Delta to expand the handle table */
99 WORD expand
; /* 1a Pointer to expand function (unused) */
100 WORD pstat
; /* 1c Pointer to status structure (unused) */
101 FARPROC16 notify WINE_PACKED
; /* 1e Pointer to LocalNotify() function */
102 WORD lock
; /* 22 Lock count for the heap */
103 WORD extra
; /* 24 Extra bytes to allocate when expanding */
104 WORD minsize
; /* 26 Minimum size of the heap */
105 WORD magic
; /* 28 Magic number */
110 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
112 WORD USER_HeapSel
= 0; /* USER heap selector */
113 WORD GDI_HeapSel
= 0; /* GDI heap selector */
115 /* All local heap allocations are aligned on 4-byte boundaries */
116 #define LALIGN(word) (((word) + 3) & ~3)
118 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
119 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
120 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
121 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
123 /* determine whether the handle belongs to a fixed or a moveable block */
124 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
125 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
127 /***********************************************************************
130 * Return a pointer to the local heap, making sure it exists.
132 static LOCALHEAPINFO
*LOCAL_GetHeap( HANDLE16 ds
)
134 LOCALHEAPINFO
*pInfo
;
135 INSTANCEDATA
*ptr
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN( ds
, 0 );
136 TRACE_(local
)("Heap at %p, %04x\n", ptr
, (ptr
!= NULL
? ptr
->heap
: 0xFFFF));
137 if (!ptr
|| !ptr
->heap
) return NULL
;
138 if (IsBadReadPtr16( (SEGPTR
)MAKELONG(ptr
->heap
,ds
), sizeof(LOCALHEAPINFO
)))
140 pInfo
= (LOCALHEAPINFO
*)((char*)ptr
+ ptr
->heap
);
141 if (pInfo
->magic
!= LOCAL_HEAP_MAGIC
) return NULL
;
146 /***********************************************************************
147 * LOCAL_MakeBlockFree
149 * Make a block free, inserting it in the free-list.
150 * 'block' is the handle of the block arena; 'baseptr' points to
151 * the beginning of the data segment containing the heap.
153 static void LOCAL_MakeBlockFree( char *baseptr
, WORD block
)
155 LOCALARENA
*pArena
, *pNext
;
158 /* Mark the block as free */
160 pArena
= ARENA_PTR( baseptr
, block
);
161 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FREE
;
162 pArena
->size
= pArena
->next
- block
;
164 /* Find the next free block (last block is always free) */
169 pNext
= ARENA_PTR( baseptr
, next
);
170 if ((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) break;
174 TRACE_(local
)("Local_MakeBlockFree %04x, next %04x\n", block
, next
);
175 /* Insert the free block in the free-list */
177 pArena
->free_prev
= pNext
->free_prev
;
178 pArena
->free_next
= next
;
179 ARENA_PTR(baseptr
,pNext
->free_prev
)->free_next
= block
;
180 pNext
->free_prev
= block
;
184 /***********************************************************************
185 * LOCAL_RemoveFreeBlock
187 * Remove a block from the free-list.
188 * 'block' is the handle of the block arena; 'baseptr' points to
189 * the beginning of the data segment containing the heap.
191 static void LOCAL_RemoveFreeBlock( char *baseptr
, WORD block
)
193 /* Mark the block as fixed */
195 LOCALARENA
*pArena
= ARENA_PTR( baseptr
, block
);
196 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FIXED
;
198 /* Remove it from the list */
200 ARENA_PTR(baseptr
,pArena
->free_prev
)->free_next
= pArena
->free_next
;
201 ARENA_PTR(baseptr
,pArena
->free_next
)->free_prev
= pArena
->free_prev
;
205 /***********************************************************************
208 * Insert a new block in the heap.
209 * 'new' is the handle of the new block arena; 'baseptr' points to
210 * the beginning of the data segment containing the heap; 'prev' is
211 * the block before the new one.
213 static void LOCAL_AddBlock( char *baseptr
, WORD prev
, WORD
new )
215 LOCALARENA
*pPrev
= ARENA_PTR( baseptr
, prev
);
216 LOCALARENA
*pNew
= ARENA_PTR( baseptr
, new );
218 pNew
->prev
= (prev
& ~3) | LOCAL_ARENA_FIXED
;
219 pNew
->next
= pPrev
->next
;
220 ARENA_PTR(baseptr
,pPrev
->next
)->prev
&= 3;
221 ARENA_PTR(baseptr
,pPrev
->next
)->prev
|= new;
226 /***********************************************************************
229 * Remove a block from the heap.
230 * 'block' is the handle of the block arena; 'baseptr' points to
231 * the beginning of the data segment containing the heap.
233 static void LOCAL_RemoveBlock( char *baseptr
, WORD block
)
235 LOCALARENA
*pArena
, *pTmp
;
237 /* Remove the block from the free-list */
239 TRACE_(local
)("Local_RemoveBlock\n");
240 pArena
= ARENA_PTR( baseptr
, block
);
241 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
242 LOCAL_RemoveFreeBlock( baseptr
, block
);
244 /* If the previous block is free, expand its size */
246 pTmp
= ARENA_PTR( baseptr
, pArena
->prev
& ~3 );
247 if ((pTmp
->prev
& 3) == LOCAL_ARENA_FREE
)
248 pTmp
->size
+= pArena
->next
- block
;
250 /* Remove the block from the linked list */
252 pTmp
->next
= pArena
->next
;
253 pTmp
= ARENA_PTR( baseptr
, pArena
->next
);
254 pTmp
->prev
= (pTmp
->prev
& 3) | (pArena
->prev
& ~3);
258 /***********************************************************************
261 static void LOCAL_PrintHeap( HANDLE16 ds
)
264 LOCALHEAPINFO
*pInfo
;
267 /* FIXME - the test should be done when calling the function!
268 plus is not clear that we should print this info
269 only when TRACE_ON is on! */
270 if(!TRACE_ON(local
)) return;
272 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
273 pInfo
= LOCAL_GetHeap( ds
);
277 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds
);
280 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
281 ds
, pInfo
->first
, pInfo
->last
, pInfo
->items
);
283 arena
= pInfo
->first
;
286 LOCALARENA
*pArena
= ARENA_PTR(ptr
,arena
);
287 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena
,
288 pArena
->prev
& ~3, pArena
->next
, pArena
->prev
& 3 );
289 if (arena
== pInfo
->first
)
291 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
292 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
294 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
296 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
297 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
298 if (pArena
->next
== arena
) break; /* last one */
299 if (ARENA_PTR(ptr
,pArena
->free_next
)->free_prev
!= arena
)
301 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
305 if (pArena
->next
== arena
)
307 DPRINTF( "*** last block is not marked free\n" );
310 if ((ARENA_PTR(ptr
,pArena
->next
)->prev
& ~3) != arena
)
312 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
313 pArena
->next
, ARENA_PTR(ptr
,pArena
->next
)->prev
);
316 arena
= pArena
->next
;
321 /***********************************************************************
322 * LocalInit (KERNEL.4)
324 BOOL16 WINAPI
LocalInit16( HANDLE16 selector
, WORD start
, WORD end
)
327 WORD heapInfoArena
, freeArena
, lastArena
;
328 LOCALHEAPINFO
*pHeapInfo
;
329 LOCALARENA
*pArena
, *pFirstArena
, *pLastArena
;
333 /* The initial layout of the heap is: */
334 /* - first arena (FIXED) */
335 /* - heap info structure (FIXED) */
336 /* - large free block (FREE) */
337 /* - last arena (FREE) */
339 TRACE_(local
)("%04x %04x-%04x\n", selector
, start
, end
);
340 if (!selector
) selector
= CURRENT_DS
;
344 /* If TRACE_ON(heap) is set, the global heap blocks are */
345 /* cleared before use, so we can test for double initialization. */
346 if (LOCAL_GetHeap(selector
))
348 ERR_(local
)("Heap %04x initialized twice.\n", selector
);
349 LOCAL_PrintHeap(selector
);
355 /* start == 0 means: put the local heap at the end of the segment */
357 DWORD size
= GlobalSize16( GlobalHandle16( selector
) );
358 start
= (WORD
)(size
> 0xffff ? 0xffff : size
) - 1;
359 if ( end
> 0xfffe ) end
= 0xfffe;
365 if ((pModule
= NE_GetPtr( GlobalHandle16( selector
) )))
367 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
);
370 for ( segNr
= 0; segNr
< pModule
->seg_count
; segNr
++, pSeg
++ )
371 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
374 if ( segNr
< pModule
->seg_count
)
376 WORD minsize
= pSeg
->minsize
;
377 if ( pModule
->ss
== segNr
+1 )
378 minsize
+= pModule
->stack_size
;
380 TRACE_(local
)(" new start %04x, minstart: %04x\n", start
, minsize
);
384 ptr
= PTR_SEG_OFF_TO_LIN( selector
, 0 );
386 start
= LALIGN( MAX( start
, sizeof(INSTANCEDATA
) ) );
387 heapInfoArena
= LALIGN(start
+ sizeof(LOCALARENA
) );
388 freeArena
= LALIGN( heapInfoArena
+ ARENA_HEADER_SIZE
389 + sizeof(LOCALHEAPINFO
) );
390 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
392 /* Make sure there's enough space. */
394 if (freeArena
+ sizeof(LOCALARENA
) >= lastArena
) goto done
;
396 /* Initialise the first arena */
398 pFirstArena
= ARENA_PTR( ptr
, start
);
399 pFirstArena
->prev
= start
| LOCAL_ARENA_FIXED
;
400 pFirstArena
->next
= heapInfoArena
;
401 pFirstArena
->size
= LALIGN(sizeof(LOCALARENA
));
402 pFirstArena
->free_prev
= start
; /* this one */
403 pFirstArena
->free_next
= freeArena
;
405 /* Initialise the arena of the heap info structure */
407 pArena
= ARENA_PTR( ptr
, heapInfoArena
);
408 pArena
->prev
= start
| LOCAL_ARENA_FIXED
;
409 pArena
->next
= freeArena
;
411 /* Initialise the heap info structure */
413 pHeapInfo
= (LOCALHEAPINFO
*) (ptr
+ heapInfoArena
+ ARENA_HEADER_SIZE
);
414 memset( pHeapInfo
, 0, sizeof(LOCALHEAPINFO
) );
415 pHeapInfo
->items
= 4;
416 pHeapInfo
->first
= start
;
417 pHeapInfo
->last
= lastArena
;
418 pHeapInfo
->htable
= 0;
419 pHeapInfo
->hdelta
= 0x20;
420 pHeapInfo
->extra
= 0x200;
421 pHeapInfo
->minsize
= lastArena
- freeArena
;
422 pHeapInfo
->magic
= LOCAL_HEAP_MAGIC
;
424 /* Initialise the large free block */
426 pArena
= ARENA_PTR( ptr
, freeArena
);
427 pArena
->prev
= heapInfoArena
| LOCAL_ARENA_FREE
;
428 pArena
->next
= lastArena
;
429 pArena
->size
= lastArena
- freeArena
;
430 pArena
->free_prev
= start
;
431 pArena
->free_next
= lastArena
;
433 /* Initialise the last block */
435 pLastArena
= ARENA_PTR( ptr
, lastArena
);
436 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
437 pLastArena
->next
= lastArena
; /* this one */
438 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
439 pLastArena
->free_prev
= freeArena
;
440 pLastArena
->free_next
= lastArena
; /* this one */
442 /* Store the local heap address in the instance data */
444 ((INSTANCEDATA
*)ptr
)->heap
= heapInfoArena
+ ARENA_HEADER_SIZE
;
445 LOCAL_PrintHeap( selector
);
449 CURRENT_STACK16
->ecx
= ret
; /* must be returned in cx too */
453 /***********************************************************************
456 static BOOL16
LOCAL_GrowHeap( HANDLE16 ds
)
461 LOCALHEAPINFO
*pHeapInfo
;
462 WORD freeArena
, lastArena
;
463 LOCALARENA
*pArena
, *pLastArena
;
466 hseg
= GlobalHandle16( ds
);
467 /* maybe mem allocated by Virtual*() ? */
468 if (!hseg
) return FALSE
;
470 oldsize
= GlobalSize16( hseg
);
471 /* if nothing can be gained, return */
472 if (oldsize
> 0xfff0) return FALSE
;
473 hseg
= GlobalReAlloc16( hseg
, 0x10000, GMEM_FIXED
);
474 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
475 pHeapInfo
= LOCAL_GetHeap( ds
);
476 if (pHeapInfo
== NULL
) {
477 ERR_(local
)("Heap not found\n" );
480 end
= GlobalSize16( hseg
);
481 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
483 /* Update the HeapInfo */
485 freeArena
= pHeapInfo
->last
;
486 pHeapInfo
->last
= lastArena
;
487 pHeapInfo
->minsize
+= end
- oldsize
;
489 /* grow the old last block */
490 pArena
= ARENA_PTR( ptr
, freeArena
);
491 pArena
->size
= lastArena
- freeArena
;
492 pArena
->next
= lastArena
;
493 pArena
->free_next
= lastArena
;
495 /* Initialise the new last block */
497 pLastArena
= ARENA_PTR( ptr
, lastArena
);
498 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
499 pLastArena
->next
= lastArena
; /* this one */
500 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
501 pLastArena
->free_prev
= freeArena
;
502 pLastArena
->free_next
= lastArena
; /* this one */
504 /* If block before freeArena is also free then merge them */
505 if((ARENA_PTR(ptr
, (pArena
->prev
& ~3))->prev
& 3) == LOCAL_ARENA_FREE
)
507 LOCAL_RemoveBlock(ptr
, freeArena
);
511 TRACE_(local
)("Heap expanded\n" );
512 LOCAL_PrintHeap( ds
);
517 /***********************************************************************
520 static HLOCAL16
LOCAL_FreeArena( WORD ds
, WORD arena
)
522 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
523 LOCALHEAPINFO
*pInfo
;
524 LOCALARENA
*pArena
, *pPrev
, *pNext
;
526 TRACE_(local
)("%04x ds=%04x\n", arena
, ds
);
527 if (!(pInfo
= LOCAL_GetHeap( ds
))) return arena
;
529 pArena
= ARENA_PTR( ptr
, arena
);
530 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
532 /* shouldn't happen */
533 ERR_(local
)("Trying to free block %04x twice!\n",
535 LOCAL_PrintHeap( ds
);
539 /* Check if we can merge with the previous block */
541 pPrev
= ARENA_PTR( ptr
, pArena
->prev
& ~3 );
542 pNext
= ARENA_PTR( ptr
, pArena
->next
);
543 if ((pPrev
->prev
& 3) == LOCAL_ARENA_FREE
)
545 arena
= pArena
->prev
& ~3;
547 LOCAL_RemoveBlock( ptr
, pPrev
->next
);
550 else /* Make a new free block */
552 LOCAL_MakeBlockFree( ptr
, arena
);
555 /* Check if we can merge with the next block */
557 if ((pArena
->next
== pArena
->free_next
) &&
558 (pArena
->next
!= pInfo
->last
))
560 LOCAL_RemoveBlock( ptr
, pArena
->next
);
567 /***********************************************************************
570 * Shrink an arena by creating a free block at its end if possible.
571 * 'size' includes the arena header, and must be aligned.
573 static void LOCAL_ShrinkArena( WORD ds
, WORD arena
, WORD size
)
575 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
576 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
578 if (arena
+ size
+ LALIGN(sizeof(LOCALARENA
)) < pArena
->next
)
580 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
582 LOCAL_AddBlock( ptr
, arena
, arena
+ size
);
584 LOCAL_FreeArena( ds
, arena
+ size
);
589 /***********************************************************************
590 * LOCAL_GrowArenaDownward
592 * Grow an arena downward by using the previous arena (must be free).
594 static void LOCAL_GrowArenaDownward( WORD ds
, WORD arena
, WORD newsize
)
596 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
597 LOCALHEAPINFO
*pInfo
;
598 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
599 WORD prevArena
= pArena
->prev
& ~3;
600 LOCALARENA
*pPrevArena
= ARENA_PTR( ptr
, prevArena
);
604 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
605 offset
= pPrevArena
->size
;
606 size
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
607 LOCAL_RemoveFreeBlock( ptr
, prevArena
);
608 LOCAL_RemoveBlock( ptr
, arena
);
610 p
= (char *)pPrevArena
+ ARENA_HEADER_SIZE
;
611 while (offset
< size
)
613 memcpy( p
, p
+ offset
, offset
);
617 if (size
) memcpy( p
, p
+ offset
, size
);
618 LOCAL_ShrinkArena( ds
, prevArena
, newsize
);
623 /***********************************************************************
624 * LOCAL_GrowArenaUpward
626 * Grow an arena upward by using the next arena (must be free and big
627 * enough). Newsize includes the arena header and must be aligned.
629 static void LOCAL_GrowArenaUpward( WORD ds
, WORD arena
, WORD newsize
)
631 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
632 LOCALHEAPINFO
*pInfo
;
633 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
634 WORD nextArena
= pArena
->next
;
636 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
637 LOCAL_RemoveBlock( ptr
, nextArena
);
639 LOCAL_ShrinkArena( ds
, arena
, newsize
);
643 /***********************************************************************
646 static WORD
LOCAL_GetFreeSpace(WORD ds
, WORD countdiscard
)
648 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
649 LOCALHEAPINFO
*pInfo
;
654 if (!(pInfo
= LOCAL_GetHeap( ds
)))
656 ERR_(local
)("Local heap not found\n" );
660 arena
= pInfo
->first
;
661 pArena
= ARENA_PTR( ptr
, arena
);
662 while (arena
!= pArena
->free_next
)
664 arena
= pArena
->free_next
;
665 pArena
= ARENA_PTR( ptr
, arena
);
666 if (pArena
->size
>= freespace
) freespace
= pArena
->size
;
668 /* FIXME doesn't yet calculate space that would become free if everything
669 were discarded when countdiscard == 1 */
670 if (freespace
< ARENA_HEADER_SIZE
) freespace
= 0;
671 else freespace
-= ARENA_HEADER_SIZE
;
676 /***********************************************************************
679 WORD
LOCAL_Compact( HANDLE16 ds
, UINT16 minfree
, UINT16 flags
)
681 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
682 LOCALHEAPINFO
*pInfo
;
683 LOCALARENA
*pArena
, *pMoveArena
, *pFinalArena
;
684 WORD arena
, movearena
, finalarena
, table
;
685 WORD count
, movesize
, size
;
687 LOCALHANDLEENTRY
*pEntry
;
689 if (!(pInfo
= LOCAL_GetHeap( ds
)))
691 ERR_(local
)("Local heap not found\n" );
695 TRACE_(local
)("ds = %04x, minfree = %04x, flags = %04x\n",
697 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
698 if(freespace
>= minfree
|| (flags
& LMEM_NOCOMPACT
))
700 TRACE_(local
)("Returning %04x.\n", freespace
);
703 TRACE_(local
)("Compacting heap %04x.\n", ds
);
704 table
= pInfo
->htable
;
707 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
708 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
710 if((pEntry
->lock
== 0) && (pEntry
->flags
!= (LMEM_DISCARDED
>> 8)))
712 /* OK we can move this one if we want */
713 TRACE_(local
)("handle %04x (block %04x) can be moved.\n",
714 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
715 movearena
= ARENA_HEADER(pEntry
->addr
);
716 pMoveArena
= ARENA_PTR(ptr
, movearena
);
717 movesize
= pMoveArena
->next
- movearena
;
718 arena
= pInfo
->first
;
719 pArena
= ARENA_PTR(ptr
, arena
);
722 /* Try to find the smallest arena that will do, */
723 /* which is below us in memory */
726 arena
= pArena
->free_next
;
727 pArena
= ARENA_PTR(ptr
, arena
);
728 if(arena
>= movearena
)
730 if(arena
== pArena
->free_next
)
732 if((pArena
->size
>= movesize
) && (pArena
->size
< size
))
738 if (finalarena
) /* Actually got somewhere to move */
740 TRACE_(local
)("Moving it to %04x.\n", finalarena
);
741 pFinalArena
= ARENA_PTR(ptr
, finalarena
);
742 size
= pFinalArena
->size
;
743 LOCAL_RemoveFreeBlock(ptr
, finalarena
);
744 LOCAL_ShrinkArena( ds
, finalarena
, movesize
);
745 /* Copy the arena to it's new location */
746 memcpy((char *)pFinalArena
+ ARENA_HEADER_SIZE
,
747 (char *)pMoveArena
+ ARENA_HEADER_SIZE
,
748 movesize
- ARENA_HEADER_SIZE
);
749 /* Free the old location */
750 LOCAL_FreeArena(ds
, movearena
);
752 Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_MOVE
,
753 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
754 /* Update handle table entry */
755 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ sizeof(HLOCAL16
) ;
757 else if((ARENA_PTR(ptr
, pMoveArena
->prev
& ~3)->prev
& 3)
760 /* Previous arena is free (but < movesize) */
761 /* so we can 'slide' movearena down into it */
762 finalarena
= pMoveArena
->prev
& ~3;
763 LOCAL_GrowArenaDownward( ds
, movearena
, movesize
);
764 /* Update handle table entry */
765 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ sizeof(HLOCAL16
) ;
769 table
= *(WORD
*)pEntry
;
771 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
772 if(freespace
>= minfree
|| (flags
& LMEM_NODISCARD
))
774 TRACE_(local
)("Returning %04x.\n", freespace
);
778 table
= pInfo
->htable
;
781 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
782 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
784 if(pEntry
->addr
&& pEntry
->lock
== 0 &&
785 (pEntry
->flags
& (LMEM_DISCARDABLE
>> 8)))
787 TRACE_(local
)("Discarding handle %04x (block %04x).\n",
788 (char *)pEntry
- ptr
, pEntry
->addr
);
789 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
));
791 Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_DISCARD
,
792 (char *)pEntry
- ptr
, pEntry
->flags
);
794 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
797 table
= *(WORD
*)pEntry
;
799 return LOCAL_Compact(ds
, 0xffff, LMEM_NODISCARD
);
803 /***********************************************************************
804 * LOCAL_FindFreeBlock
806 static HLOCAL16
LOCAL_FindFreeBlock( HANDLE16 ds
, WORD size
)
808 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
809 LOCALHEAPINFO
*pInfo
;
813 if (!(pInfo
= LOCAL_GetHeap( ds
)))
815 ERR_(local
)("Local heap not found\n" );
820 arena
= pInfo
->first
;
821 pArena
= ARENA_PTR( ptr
, arena
);
823 arena
= pArena
->free_next
;
824 pArena
= ARENA_PTR( ptr
, arena
);
825 if (arena
== pArena
->free_next
) break;
826 if (pArena
->size
>= size
) return arena
;
828 TRACE_(local
)("not enough space\n" );
833 /***********************************************************************
835 * The segment may get moved around in this function, so all callers
836 * should reset their pointer variables.
838 static HLOCAL16
LOCAL_GetBlock( HANDLE16 ds
, WORD size
, WORD flags
)
840 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
841 LOCALHEAPINFO
*pInfo
;
845 if (!(pInfo
= LOCAL_GetHeap( ds
)))
847 ERR_(local
)("Local heap not found\n");
852 size
+= ARENA_HEADER_SIZE
;
853 size
= LALIGN( MAX( size
, sizeof(LOCALARENA
) ) );
858 /* Find a suitable free block */
859 arena
= LOCAL_FindFreeBlock( ds
, size
);
861 /* no space: try to make some */
862 LOCAL_Compact( ds
, size
, flags
);
863 arena
= LOCAL_FindFreeBlock( ds
, size
);
866 /* still no space: try to grow the segment */
867 if (!(LOCAL_GrowHeap( ds
)))
870 /* FIXME: doesn't work correctly yet */
871 if ((pInfo
->notify
) && (Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_OUTOFMEM
, ds
- 20, size
))) /* FIXME: "size" correct ? (should indicate bytes needed) */
876 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
877 pInfo
= LOCAL_GetHeap( ds
);
878 arena
= LOCAL_FindFreeBlock( ds
, size
);
881 if (ds
== GDI_HeapSel
) {
882 ERR_(local
)("not enough space in GDI local heap "
883 "(%04x) for %d bytes\n", ds
, size
);
884 } else if (ds
== USER_HeapSel
) {
885 ERR_(local
)("not enough space in USER local heap "
886 "(%04x) for %d bytes\n", ds
, size
);
888 ERR_(local
)("not enough space in local heap "
889 "%04x for %d bytes\n", ds
, size
);
892 if ((pInfo
->notify
) &&
893 /* FIXME: "size" correct ? (should indicate bytes needed) */
894 (Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_OUTOFMEM
, ds
, size
)))
900 /* Make a block out of the free arena */
901 pArena
= ARENA_PTR( ptr
, arena
);
902 TRACE_(local
)("LOCAL_GetBlock size = %04x, arena %04x size %04x\n",
903 size
, arena
, pArena
->size
);
904 LOCAL_RemoveFreeBlock( ptr
, arena
);
905 LOCAL_ShrinkArena( ds
, arena
, size
);
907 if (flags
& LMEM_ZEROINIT
)
908 memset((char *)pArena
+ ARENA_HEADER_SIZE
, 0, size
-ARENA_HEADER_SIZE
);
909 return arena
+ ARENA_HEADER_SIZE
;
913 /***********************************************************************
916 static BOOL16
LOCAL_NewHTable( HANDLE16 ds
)
918 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
919 LOCALHEAPINFO
*pInfo
;
920 LOCALHANDLEENTRY
*pEntry
;
924 TRACE_(local
)("Local_NewHTable\n" );
925 if (!(pInfo
= LOCAL_GetHeap( ds
)))
927 ERR_(local
)("Local heap not found\n");
932 if (!(handle
= LOCAL_GetBlock( ds
, pInfo
->hdelta
* sizeof(LOCALHANDLEENTRY
)
933 + 2 * sizeof(WORD
), LMEM_FIXED
)))
935 if (!(ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 )))
936 ERR_(local
)("ptr == NULL after GetBlock.\n");
937 if (!(pInfo
= LOCAL_GetHeap( ds
)))
938 ERR_(local
)("pInfo == NULL after GetBlock.\n");
940 /* Fill the entry table */
942 *(WORD
*)(ptr
+ handle
) = pInfo
->hdelta
;
943 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
+ sizeof(WORD
));
944 for (i
= pInfo
->hdelta
; i
> 0; i
--, pEntry
++) {
945 pEntry
->lock
= pEntry
->flags
= 0xff;
948 *(WORD
*)pEntry
= pInfo
->htable
;
949 pInfo
->htable
= handle
;
954 /***********************************************************************
955 * LOCAL_GetNewHandleEntry
957 static HLOCAL16
LOCAL_GetNewHandleEntry( HANDLE16 ds
)
959 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
960 LOCALHEAPINFO
*pInfo
;
961 LOCALHANDLEENTRY
*pEntry
= NULL
;
964 if (!(pInfo
= LOCAL_GetHeap( ds
)))
966 ERR_(local
)("Local heap not found\n");
971 /* Find a free slot in existing tables */
973 table
= pInfo
->htable
;
976 WORD count
= *(WORD
*)(ptr
+ table
);
977 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
978 for (; count
> 0; count
--, pEntry
++)
979 if (pEntry
->lock
== 0xff) break;
981 table
= *(WORD
*)pEntry
;
984 if (!table
) /* We need to create a new table */
986 if (!LOCAL_NewHTable( ds
)) return 0;
987 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
988 pInfo
= LOCAL_GetHeap( ds
);
989 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ pInfo
->htable
+ sizeof(WORD
));
992 /* Now allocate this entry */
996 TRACE_(local
)("(%04x): %04x\n",
997 ds
, ((char *)pEntry
- ptr
) );
998 return (HLOCAL16
)((char *)pEntry
- ptr
);
1002 /***********************************************************************
1003 * LOCAL_FreeHandleEntry
1005 * Free a handle table entry.
1007 static void LOCAL_FreeHandleEntry( HANDLE16 ds
, HLOCAL16 handle
)
1009 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1010 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1011 LOCALHEAPINFO
*pInfo
;
1013 WORD table
, count
, i
;
1015 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
1017 /* Find the table where this handle comes from */
1019 pTable
= &pInfo
->htable
;
1022 WORD size
= (*(WORD
*)(ptr
+ *pTable
)) * sizeof(LOCALHANDLEENTRY
);
1023 if ((handle
>= *pTable
+ sizeof(WORD
)) &&
1024 (handle
< *pTable
+ sizeof(WORD
) + size
)) break; /* Found it */
1025 pTable
= (WORD
*)(ptr
+ *pTable
+ sizeof(WORD
) + size
);
1029 ERR_(local
)("Invalid entry %04x\n", handle
);
1030 LOCAL_PrintHeap( ds
);
1034 /* Make the entry free */
1036 pEntry
->addr
= 0; /* just in case */
1037 pEntry
->lock
= 0xff;
1038 pEntry
->flags
= 0xff;
1039 /* Now check if all entries in this table are free */
1042 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1043 count
= *(WORD
*)(ptr
+ table
);
1044 for (i
= count
; i
> 0; i
--, pEntry
++) if (pEntry
->lock
!= 0xff) return;
1046 /* Remove the table from the linked list and free it */
1048 TRACE_(local
)("(%04x): freeing table %04x\n",
1050 *pTable
= *(WORD
*)pEntry
;
1051 LOCAL_FreeArena( ds
, ARENA_HEADER( table
) );
1055 /***********************************************************************
1058 * Implementation of LocalFree().
1060 HLOCAL16
LOCAL_Free( HANDLE16 ds
, HLOCAL16 handle
)
1062 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1064 TRACE_(local
)("%04x ds=%04x\n", handle
, ds
);
1066 if (!handle
) { WARN_(local
)("Handle is 0.\n" ); return 0; }
1067 if (HANDLE_FIXED( handle
))
1069 if (!LOCAL_FreeArena( ds
, ARENA_HEADER( handle
) )) return 0; /* OK */
1070 else return handle
; /* couldn't free it */
1074 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1075 if (pEntry
->flags
!= (LMEM_DISCARDED
>> 8))
1077 TRACE_(local
)("real block at %04x\n",
1079 if (LOCAL_FreeArena( ds
, ARENA_HEADER(pEntry
->addr
) ))
1080 return handle
; /* couldn't free it */
1082 LOCAL_FreeHandleEntry( ds
, handle
);
1088 /***********************************************************************
1091 * Implementation of LocalAlloc().
1094 HLOCAL16
LOCAL_Alloc( HANDLE16 ds
, WORD flags
, WORD size
)
1099 TRACE_(local
)("%04x %d ds=%04x\n", flags
, size
, ds
);
1101 if(size
> 0 && size
<= 4) size
= 5;
1102 if (flags
& LMEM_MOVEABLE
)
1104 LOCALHANDLEENTRY
*plhe
;
1109 if (!(hmem
= LOCAL_GetBlock( ds
, size
+ sizeof(HLOCAL16
), flags
)))
1112 else /* We just need to allocate a discarded handle */
1114 if (!(handle
= LOCAL_GetNewHandleEntry( ds
)))
1116 WARN_(local
)("Couldn't get handle.\n");
1118 LOCAL_FreeArena( ds
, ARENA_HEADER(hmem
) );
1121 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1122 plhe
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1126 plhe
->addr
= hmem
+ sizeof(HLOCAL16
);
1127 plhe
->flags
= (BYTE
)((flags
& 0x0f00) >> 8);
1128 *(HLOCAL16
*)(ptr
+ hmem
) = handle
;
1133 plhe
->flags
= LMEM_DISCARDED
>> 8;
1140 handle
= LOCAL_GetBlock( ds
, size
, flags
);
1146 /***********************************************************************
1149 * Implementation of LocalReAlloc().
1151 HLOCAL16
LOCAL_ReAlloc( HANDLE16 ds
, HLOCAL16 handle
, WORD size
, WORD flags
)
1153 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1154 LOCALHEAPINFO
*pInfo
;
1155 LOCALARENA
*pArena
, *pNext
;
1156 LOCALHANDLEENTRY
*pEntry
= NULL
;
1157 WORD arena
, oldsize
;
1158 HLOCAL16 hmem
, blockhandle
;
1161 if (!handle
) return 0;
1162 if(HANDLE_MOVEABLE(handle
) &&
1163 ((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
== 0xff) /* An unused handle */
1166 TRACE_(local
)("%04x %d %04x ds=%04x\n",
1167 handle
, size
, flags
, ds
);
1168 if (!(pInfo
= LOCAL_GetHeap( ds
))) return 0;
1170 if (HANDLE_FIXED( handle
))
1171 blockhandle
= handle
;
1174 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1175 if(pEntry
->flags
== (LMEM_DISCARDED
>> 8))
1179 WARN_(local
)("Dicarded block has non-zero addr.\n");
1180 TRACE_(local
)("ReAllocating discarded block\n");
1181 if(size
<= 4) size
= 5;
1182 if (!(hl
= LOCAL_GetBlock( ds
, size
+ sizeof(HLOCAL16
), flags
)))
1184 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 ); /* Reload ptr */
1185 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1186 pEntry
->addr
= hl
+ sizeof(HLOCAL16
);
1189 *(HLOCAL16
*)(ptr
+ hl
) = handle
;
1192 if (((blockhandle
= pEntry
->addr
) & 3) != 2)
1194 ERR_(local
)("(%04x,%04x): invalid handle\n",
1198 if(*((HLOCAL16
*)(ptr
+ blockhandle
) - 1) != handle
) {
1199 ERR_(local
)("Back ptr to handle is invalid\n");
1204 if (flags
& LMEM_MODIFY
)
1206 if (HANDLE_MOVEABLE(handle
))
1208 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1209 pEntry
->flags
= (flags
& 0x0f00) >> 8;
1210 TRACE_(local
)("Changing flags to %x.\n", pEntry
->flags
);
1217 if (flags
& LMEM_MOVEABLE
)
1219 if (HANDLE_FIXED(handle
))
1221 TRACE_(local
)("Freeing fixed block.\n");
1222 return LOCAL_Free( ds
, handle
);
1224 else /* Moveable block */
1226 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1227 if (pEntry
->lock
== 0)
1229 /* discards moveable blocks */
1230 TRACE_(local
)("Discarding block\n");
1231 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
));
1233 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
1241 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1242 if (pEntry
->lock
== 0)
1245 return LOCAL_Free( ds
, handle
);
1251 arena
= ARENA_HEADER( blockhandle
);
1252 TRACE_(local
)("arena is %04x\n", arena
);
1253 pArena
= ARENA_PTR( ptr
, arena
);
1255 if(size
<= 4) size
= 5;
1256 oldsize
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
1257 nextarena
= LALIGN(blockhandle
+ size
);
1259 /* Check for size reduction */
1261 if (nextarena
<= pArena
->next
)
1263 TRACE_(local
)("size reduction, making new free block\n");
1264 LOCAL_ShrinkArena(ds
, arena
, nextarena
- arena
);
1265 TRACE_(local
)("returning %04x\n", handle
);
1269 /* Check if the next block is free and large enough */
1271 pNext
= ARENA_PTR( ptr
, pArena
->next
);
1272 if (((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) &&
1273 (nextarena
<= pNext
->next
))
1275 TRACE_(local
)("size increase, making new free block\n");
1276 LOCAL_GrowArenaUpward(ds
, arena
, nextarena
- arena
);
1277 TRACE_(local
)("returning %04x\n", handle
);
1281 /* Now we have to allocate a new block, but not if (fixed block or locked
1282 block) and no LMEM_MOVEABLE */
1284 if (!(flags
& LMEM_MOVEABLE
))
1286 if (HANDLE_FIXED(handle
))
1288 ERR_(local
)("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1293 if(((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
!= 0)
1295 ERR_(local
)("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1300 if(HANDLE_MOVEABLE(handle
)) size
+= sizeof(HLOCAL16
);
1301 hmem
= LOCAL_GetBlock( ds
, size
, flags
);
1302 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 ); /* Reload ptr */
1303 if(HANDLE_MOVEABLE(handle
)) /* LOCAL_GetBlock might have triggered */
1304 { /* a compaction, which might in turn have */
1305 blockhandle
= pEntry
->addr
; /* moved the very block we are resizing */
1306 arena
= ARENA_HEADER( blockhandle
); /* thus, we reload arena, too */
1310 /* Remove the block from the heap and try again */
1311 LPSTR buffer
= HeapAlloc( GetProcessHeap(), 0, oldsize
);
1312 if (!buffer
) return 0;
1313 memcpy( buffer
, ptr
+ arena
+ ARENA_HEADER_SIZE
, oldsize
);
1314 LOCAL_FreeArena( ds
, arena
);
1315 if (!(hmem
= LOCAL_GetBlock( ds
, size
, flags
)))
1317 if (!(hmem
= LOCAL_GetBlock( ds
, oldsize
, flags
)))
1319 ERR_(local
)("Can't restore saved block\n" );
1320 HeapFree( GetProcessHeap(), 0, buffer
);
1325 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 ); /* Reload ptr */
1326 memcpy( ptr
+ hmem
, buffer
, oldsize
);
1327 HeapFree( GetProcessHeap(), 0, buffer
);
1331 memcpy( ptr
+ hmem
, ptr
+ (arena
+ ARENA_HEADER_SIZE
), oldsize
);
1332 LOCAL_FreeArena( ds
, arena
);
1334 if (HANDLE_MOVEABLE( handle
))
1336 TRACE_(local
)("fixing handle\n");
1337 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1338 pEntry
->addr
= hmem
+ sizeof(HLOCAL16
);
1339 /* Back ptr should still be correct */
1340 if(*(HLOCAL16
*)(ptr
+ hmem
) != handle
)
1341 ERR_(local
)("back ptr is invalid.\n");
1344 if (size
== oldsize
) hmem
= 0; /* Realloc failed */
1345 TRACE_(local
)("returning %04x\n", hmem
);
1350 /***********************************************************************
1351 * LOCAL_InternalLock
1353 static HLOCAL16
LOCAL_InternalLock( LPSTR heap
, HLOCAL16 handle
)
1355 HLOCAL16 old_handle
= handle
;
1357 if (HANDLE_MOVEABLE(handle
))
1359 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(heap
+ handle
);
1360 if (pEntry
->flags
== LMEM_DISCARDED
) return 0;
1361 if (pEntry
->lock
< 0xfe) pEntry
->lock
++;
1362 handle
= pEntry
->addr
;
1364 TRACE_(local
)("%04x returning %04x\n",
1365 old_handle
, handle
);
1370 /***********************************************************************
1373 LPSTR
LOCAL_Lock( HANDLE16 ds
, HLOCAL16 handle
)
1375 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1376 return handle
? ptr
+ LOCAL_InternalLock( ptr
, handle
) : NULL
;
1380 /***********************************************************************
1383 SEGPTR
LOCAL_LockSegptr( HANDLE16 ds
, HLOCAL16 handle
)
1385 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1386 return PTR_SEG_OFF_TO_SEGPTR( ds
, LOCAL_InternalLock( ptr
, handle
) );
1390 /***********************************************************************
1393 BOOL16
LOCAL_Unlock( HANDLE16 ds
, HLOCAL16 handle
)
1395 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1397 TRACE_(local
)("%04x\n", handle
);
1398 if (HANDLE_MOVEABLE(handle
))
1400 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1401 if (!pEntry
->lock
|| (pEntry
->lock
== 0xff)) return FALSE
;
1402 /* For moveable block, return the new lock count */
1403 /* (see _Windows_Internals_ p. 197) */
1404 return --pEntry
->lock
;
1410 /***********************************************************************
1413 * Implementation of LocalSize().
1415 WORD
LOCAL_Size( HANDLE16 ds
, HLOCAL16 handle
)
1417 char *ptr
= PTR_SEG_OFF_TO_LIN( CURRENT_DS
, 0 );
1420 TRACE_(local
)("%04x ds=%04x\n", handle
, ds
);
1422 if (HANDLE_MOVEABLE( handle
)) handle
= *(WORD
*)(ptr
+ handle
);
1423 if (!handle
) return 0;
1424 pArena
= ARENA_PTR( ptr
, ARENA_HEADER(handle
) );
1425 return pArena
->next
- handle
;
1429 /***********************************************************************
1432 * Implementation of LocalFlags().
1434 WORD
LOCAL_Flags( HANDLE16 ds
, HLOCAL16 handle
)
1436 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1438 if (HANDLE_MOVEABLE(handle
))
1440 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1441 TRACE_(local
)("(%04x,%04x): returning %04x\n",
1442 ds
, handle
, pEntry
->lock
| (pEntry
->flags
<< 8) );
1443 return pEntry
->lock
| (pEntry
->flags
<< 8);
1447 TRACE_(local
)("(%04x,%04x): returning 0\n",
1454 /***********************************************************************
1457 * Implementation of LocalHeapSize().
1459 WORD
LOCAL_HeapSize( HANDLE16 ds
)
1461 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1462 if (!pInfo
) return 0;
1463 return pInfo
->last
- pInfo
->first
;
1467 /***********************************************************************
1470 * Implementation of LocalCountFree().
1472 WORD
LOCAL_CountFree( HANDLE16 ds
)
1476 LOCALHEAPINFO
*pInfo
;
1477 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1479 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1481 ERR_(local
)("(%04x): Local heap not found\n", ds
);
1482 LOCAL_PrintHeap( ds
);
1487 arena
= pInfo
->first
;
1488 pArena
= ARENA_PTR( ptr
, arena
);
1491 arena
= pArena
->free_next
;
1492 pArena
= ARENA_PTR( ptr
, arena
);
1493 if (arena
== pArena
->free_next
) break;
1494 total
+= pArena
->size
;
1496 TRACE_(local
)("(%04x): returning %d\n", ds
, total
);
1501 /***********************************************************************
1504 * Implementation of LocalHandle().
1506 HLOCAL16
LOCAL_Handle( HANDLE16 ds
, WORD addr
)
1508 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1509 LOCALHEAPINFO
*pInfo
;
1512 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1514 ERR_(local
)("(%04x): Local heap not found\n", ds
);
1515 LOCAL_PrintHeap( ds
);
1519 /* Find the address in the entry tables */
1521 table
= pInfo
->htable
;
1524 WORD count
= *(WORD
*)(ptr
+ table
);
1525 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+table
+sizeof(WORD
));
1526 for (; count
> 0; count
--, pEntry
++)
1527 if (pEntry
->addr
== addr
) return (HLOCAL16
)((char *)pEntry
- ptr
);
1528 table
= *(WORD
*)pEntry
;
1531 return (HLOCAL16
)addr
; /* Fixed block handle is addr */
1535 /***********************************************************************
1536 * LocalAlloc16 (KERNEL.5)
1538 HLOCAL16 WINAPI
LocalAlloc16( UINT16 flags
, WORD size
)
1540 HLOCAL16 ret
= LOCAL_Alloc( CURRENT_DS
, flags
, size
);
1541 CURRENT_STACK16
->ecx
= ret
; /* must be returned in cx too */
1546 /***********************************************************************
1547 * LocalReAlloc16 (KERNEL.6)
1549 HLOCAL16 WINAPI
LocalReAlloc16( HLOCAL16 handle
, WORD size
, UINT16 flags
)
1551 return LOCAL_ReAlloc( CURRENT_DS
, handle
, size
, flags
);
1555 /***********************************************************************
1556 * LocalFree16 (KERNEL.7)
1558 HLOCAL16 WINAPI
LocalFree16( HLOCAL16 handle
)
1560 return LOCAL_Free( CURRENT_DS
, handle
);
1564 /***********************************************************************
1565 * LocalLock16 (KERNEL.8)
1567 * Note: only the offset part of the pointer is returned by the relay code.
1569 SEGPTR WINAPI
LocalLock16( HLOCAL16 handle
)
1571 return LOCAL_LockSegptr( CURRENT_DS
, handle
);
1575 /***********************************************************************
1576 * LocalUnlock16 (KERNEL.9)
1578 BOOL16 WINAPI
LocalUnlock16( HLOCAL16 handle
)
1580 return LOCAL_Unlock( CURRENT_DS
, handle
);
1584 /***********************************************************************
1585 * LocalSize16 (KERNEL.10)
1587 UINT16 WINAPI
LocalSize16( HLOCAL16 handle
)
1589 return LOCAL_Size( CURRENT_DS
, handle
);
1593 /***********************************************************************
1594 * LocalHandle16 (KERNEL.11)
1596 HLOCAL16 WINAPI
LocalHandle16( WORD addr
)
1598 return LOCAL_Handle( CURRENT_DS
, addr
);
1602 /***********************************************************************
1603 * LocalFlags16 (KERNEL.12)
1605 UINT16 WINAPI
LocalFlags16( HLOCAL16 handle
)
1607 return LOCAL_Flags( CURRENT_DS
, handle
);
1611 /***********************************************************************
1612 * LocalCompact16 (KERNEL.13)
1614 UINT16 WINAPI
LocalCompact16( UINT16 minfree
)
1616 TRACE_(local
)("%04x\n", minfree
);
1617 return LOCAL_Compact( CURRENT_DS
, minfree
, 0 );
1621 /***********************************************************************
1622 * LocalNotify (KERNEL.14)
1624 * Installs a callback function that is called for local memory events
1625 * Callback function prototype is
1626 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1629 * NotifyFunc seems to be responsible for allocating some memory,
1630 * returns TRUE for success.
1631 * wArg = number of bytes needed additionally
1633 * hMem = handle; wArg = old mem location
1635 * NotifyFunc seems to be strongly encouraged to return TRUE,
1636 * otherwise LogError() gets called.
1637 * hMem = handle; wArg = flags
1639 FARPROC16 WINAPI
LocalNotify16( FARPROC16 func
)
1641 LOCALHEAPINFO
*pInfo
;
1642 FARPROC16 oldNotify
;
1643 HANDLE16 ds
= CURRENT_DS
;
1645 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1647 ERR_(local
)("(%04x): Local heap not found\n", ds
);
1648 LOCAL_PrintHeap( ds
);
1651 TRACE_(local
)("(%04x): %08lx\n", ds
, (DWORD
)func
);
1652 FIXME_(local
)("Half implemented\n");
1653 oldNotify
= pInfo
->notify
;
1654 pInfo
->notify
= func
;
1659 /***********************************************************************
1660 * LocalShrink16 (KERNEL.121)
1662 UINT16 WINAPI
LocalShrink16( HGLOBAL16 handle
, UINT16 newsize
)
1664 TRACE_(local
)("%04x %04x\n", handle
, newsize
);
1669 /***********************************************************************
1670 * GetHeapSpaces (KERNEL.138)
1672 DWORD WINAPI
GetHeapSpaces16( HMODULE16 module
)
1677 if (!(pModule
= NE_GetPtr( module
))) return 0;
1679 GlobalHandleToSel16((NE_SEG_TABLE( pModule
) + pModule
->dgroup
- 1)->hSeg
);
1680 return MAKELONG( LOCAL_CountFree( ds
), LOCAL_HeapSize( ds
) );
1684 /***********************************************************************
1685 * LocalCountFree (KERNEL.161)
1687 WORD WINAPI
LocalCountFree16(void)
1689 return LOCAL_CountFree( CURRENT_DS
);
1693 /***********************************************************************
1694 * LocalHeapSize (KERNEL.162)
1696 WORD WINAPI
LocalHeapSize16(void)
1698 TRACE_(local
)("(void)\n" );
1699 return LOCAL_HeapSize( CURRENT_DS
);
1703 /***********************************************************************
1704 * LocalHandleDelta (KERNEL.310)
1706 WORD WINAPI
LocalHandleDelta16( WORD delta
)
1708 LOCALHEAPINFO
*pInfo
;
1710 if (!(pInfo
= LOCAL_GetHeap( CURRENT_DS
)))
1712 ERR_(local
)("Local heap not found\n");
1713 LOCAL_PrintHeap( CURRENT_DS
);
1716 if (delta
) pInfo
->hdelta
= delta
;
1717 TRACE_(local
)("returning %04x\n", pInfo
->hdelta
);
1718 return pInfo
->hdelta
;
1722 /***********************************************************************
1723 * LocalInfo (TOOLHELP.56)
1725 BOOL16 WINAPI
LocalInfo16( LOCALINFO
*pLocalInfo
, HGLOBAL16 handle
)
1727 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap(SELECTOROF(WIN16_GlobalLock16(handle
)));
1728 if (!pInfo
) return FALSE
;
1729 pLocalInfo
->wcItems
= pInfo
->items
;
1734 /***********************************************************************
1735 * LocalFirst (TOOLHELP.57)
1737 BOOL16 WINAPI
LocalFirst16( LOCALENTRY
*pLocalEntry
, HGLOBAL16 handle
)
1739 WORD ds
= GlobalHandleToSel16( handle
);
1740 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1741 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1742 if (!pInfo
) return FALSE
;
1744 pLocalEntry
->hHandle
= pInfo
->first
+ ARENA_HEADER_SIZE
;
1745 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1746 pLocalEntry
->wFlags
= LF_FIXED
;
1747 pLocalEntry
->wcLock
= 0;
1748 pLocalEntry
->wType
= LT_NORMAL
;
1749 pLocalEntry
->hHeap
= handle
;
1750 pLocalEntry
->wHeapType
= NORMAL_HEAP
;
1751 pLocalEntry
->wNext
= ARENA_PTR(ptr
,pInfo
->first
)->next
;
1752 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1757 /***********************************************************************
1758 * LocalNext (TOOLHELP.58)
1760 BOOL16 WINAPI
LocalNext16( LOCALENTRY
*pLocalEntry
)
1762 WORD ds
= GlobalHandleToSel16( pLocalEntry
->hHeap
);
1763 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1766 if (!LOCAL_GetHeap( ds
)) return FALSE
;
1767 if (!pLocalEntry
->wNext
) return FALSE
;
1768 pArena
= ARENA_PTR( ptr
, pLocalEntry
->wNext
);
1770 pLocalEntry
->hHandle
= pLocalEntry
->wNext
+ ARENA_HEADER_SIZE
;
1771 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1772 pLocalEntry
->wFlags
= (pArena
->prev
& 3) + 1;
1773 pLocalEntry
->wcLock
= 0;
1774 pLocalEntry
->wType
= LT_NORMAL
;
1775 if (pArena
->next
!= pLocalEntry
->wNext
) /* last one? */
1776 pLocalEntry
->wNext
= pArena
->next
;
1778 pLocalEntry
->wNext
= 0;
1779 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1784 /***********************************************************************
1785 * LocalAlloc32 (KERNEL32.371)
1790 HLOCAL WINAPI
LocalAlloc(
1791 UINT flags
, /* [in] Allocation attributes */
1792 DWORD size
/* [in] Number of bytes to allocate */
1794 return (HLOCAL
)GlobalAlloc( flags
, size
);
1798 /***********************************************************************
1799 * LocalCompact32 (KERNEL32.372)
1801 UINT WINAPI
LocalCompact( UINT minfree
)
1803 return 0; /* LocalCompact does nothing in Win32 */
1807 /***********************************************************************
1808 * LocalFlags32 (KERNEL32.374)
1810 * Value specifying allocation flags and lock count.
1811 * LMEM_INVALID_HANDLE: Failure
1813 UINT WINAPI
LocalFlags(
1814 HLOCAL handle
/* [in] Handle of memory object */
1816 return GlobalFlags( (HGLOBAL
)handle
);
1820 /***********************************************************************
1821 * LocalFree32 (KERNEL32.375)
1826 HLOCAL WINAPI
LocalFree(
1827 HLOCAL handle
/* [in] Handle of memory object */
1829 return (HLOCAL
)GlobalFree( (HGLOBAL
)handle
);
1833 /***********************************************************************
1834 * LocalHandle32 (KERNEL32.376)
1839 HLOCAL WINAPI
LocalHandle(
1840 LPCVOID ptr
/* [in] Address of local memory object */
1842 return (HLOCAL
)GlobalHandle( ptr
);
1846 /***********************************************************************
1847 * LocalLock32 (KERNEL32.377)
1848 * Locks a local memory object and returns pointer to the first byte
1849 * of the memory block.
1855 LPVOID WINAPI
LocalLock(
1856 HLOCAL handle
/* [in] Address of local memory object */
1858 return GlobalLock( (HGLOBAL
)handle
);
1862 /***********************************************************************
1863 * LocalReAlloc32 (KERNEL32.378)
1868 HLOCAL WINAPI
LocalReAlloc(
1869 HLOCAL handle
, /* [in] Handle of memory object */
1870 DWORD size
, /* [in] New size of block */
1871 UINT flags
/* [in] How to reallocate object */
1873 return (HLOCAL
)GlobalReAlloc( (HGLOBAL
)handle
, size
, flags
);
1877 /***********************************************************************
1878 * LocalShrink32 (KERNEL32.379)
1880 UINT WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
1882 return 0; /* LocalShrink does nothing in Win32 */
1886 /***********************************************************************
1887 * LocalSize32 (KERNEL32.380)
1892 UINT WINAPI
LocalSize(
1893 HLOCAL handle
/* [in] Handle of memory object */
1895 return GlobalSize( (HGLOBAL
)handle
);
1899 /***********************************************************************
1900 * LocalUnlock32 (KERNEL32.381)
1902 * TRUE: Object is still locked
1903 * FALSE: Object is unlocked
1905 BOOL WINAPI
LocalUnlock(
1906 HLOCAL handle
/* [in] Handle of memory object */
1908 return GlobalUnlock( (HGLOBAL
)handle
);