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.
25 #include "stackframe.h"
33 WORD prev
; /* Previous arena | arena type */
34 WORD next
; /* Next arena */
35 /* Start of the memory block or free-list info */
36 WORD size
; /* Size of the free block */
37 WORD free_prev
; /* Previous free block */
38 WORD free_next
; /* Next free block */
41 #define ARENA_HEADER_SIZE 4
42 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
44 /* Arena types (stored in 'prev' field of the arena) */
45 #define LOCAL_ARENA_FREE 0
46 #define LOCAL_ARENA_FIXED 1
48 /* LocalNotify() msgs */
54 /* Layout of a handle entry table
56 * WORD count of entries
57 * LOCALHANDLEENTRY[count] entries
58 * WORD near ptr to next table
62 WORD addr
; /* Address of the MOVEABLE block */
63 BYTE flags
; /* Flags for this block */
64 BYTE lock
; /* Lock count */
68 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
69 * in case something actually relies on this.
70 * Note the ARENA_HEADER(addr) still produces the desired result ie. 4n - 4
72 * An unused handle has lock = flags = 0xff. In windows addr is that of next
73 * free handle, at the moment in wine we set it to 0.
75 * A discarded block's handle has lock = addr = 0 and flags = 0x40
76 * (LMEM_DISCARDED >> 8)
83 WORD check
; /* 00 Heap checking flag */
84 WORD freeze
; /* 02 Heap frozen flag */
85 WORD items
; /* 04 Count of items on the heap */
86 WORD first
; /* 06 First item of the heap */
87 WORD pad1
; /* 08 Always 0 */
88 WORD last
; /* 0a Last item of the heap */
89 WORD pad2
; /* 0c Always 0 */
90 BYTE ncompact
; /* 0e Compactions counter */
91 BYTE dislevel
; /* 0f Discard level */
92 DWORD distotal
; /* 10 Total bytes discarded */
93 WORD htable
; /* 14 Pointer to handle table */
94 WORD hfree
; /* 16 Pointer to free handle table */
95 WORD hdelta
; /* 18 Delta to expand the handle table */
96 WORD expand
; /* 1a Pointer to expand function (unused) */
97 WORD pstat
; /* 1c Pointer to status structure (unused) */
98 FARPROC16 notify WINE_PACKED
; /* 1e Pointer to LocalNotify() function */
99 WORD lock
; /* 22 Lock count for the heap */
100 WORD extra
; /* 24 Extra bytes to allocate when expanding */
101 WORD minsize
; /* 26 Minimum size of the heap */
102 WORD magic
; /* 28 Magic number */
107 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
109 WORD USER_HeapSel
= 0; /* USER heap selector */
110 WORD GDI_HeapSel
= 0; /* GDI 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)
124 /***********************************************************************
127 * Return a pointer to the local heap, making sure it exists.
129 static LOCALHEAPINFO
*LOCAL_GetHeap( HANDLE16 ds
)
131 LOCALHEAPINFO
*pInfo
;
132 INSTANCEDATA
*ptr
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN( ds
, 0 );
133 TRACE(local
, "Heap at %p, %04x\n", ptr
, (ptr
!= NULL
? ptr
->heap
: 0xFFFF));
134 if (!ptr
|| !ptr
->heap
) return NULL
;
135 if (IsBadReadPtr16( (SEGPTR
)MAKELONG(ptr
->heap
,ds
), sizeof(LOCALHEAPINFO
)))
137 pInfo
= (LOCALHEAPINFO
*)((char*)ptr
+ ptr
->heap
);
138 if (pInfo
->magic
!= LOCAL_HEAP_MAGIC
) return NULL
;
143 /***********************************************************************
144 * LOCAL_MakeBlockFree
146 * Make a block free, inserting it in the free-list.
147 * 'block' is the handle of the block arena; 'baseptr' points to
148 * the beginning of the data segment containing the heap.
150 static void LOCAL_MakeBlockFree( char *baseptr
, WORD block
)
152 LOCALARENA
*pArena
, *pNext
;
155 /* Mark the block as free */
157 pArena
= ARENA_PTR( baseptr
, block
);
158 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FREE
;
159 pArena
->size
= pArena
->next
- block
;
161 /* Find the next free block (last block is always free) */
166 pNext
= ARENA_PTR( baseptr
, next
);
167 if ((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) break;
171 TRACE(local
, "Local_MakeBlockFree %04x, next %04x\n", block
, next
);
172 /* Insert the free block in the free-list */
174 pArena
->free_prev
= pNext
->free_prev
;
175 pArena
->free_next
= next
;
176 ARENA_PTR(baseptr
,pNext
->free_prev
)->free_next
= block
;
177 pNext
->free_prev
= block
;
181 /***********************************************************************
182 * LOCAL_RemoveFreeBlock
184 * Remove a block from the free-list.
185 * 'block' is the handle of the block arena; 'baseptr' points to
186 * the beginning of the data segment containing the heap.
188 static void LOCAL_RemoveFreeBlock( char *baseptr
, WORD block
)
190 /* Mark the block as fixed */
192 LOCALARENA
*pArena
= ARENA_PTR( baseptr
, block
);
193 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FIXED
;
195 /* Remove it from the list */
197 ARENA_PTR(baseptr
,pArena
->free_prev
)->free_next
= pArena
->free_next
;
198 ARENA_PTR(baseptr
,pArena
->free_next
)->free_prev
= pArena
->free_prev
;
202 /***********************************************************************
205 * Insert a new block in the heap.
206 * 'new' is the handle of the new block arena; 'baseptr' points to
207 * the beginning of the data segment containing the heap; 'prev' is
208 * the block before the new one.
210 static void LOCAL_AddBlock( char *baseptr
, WORD prev
, WORD
new )
212 LOCALARENA
*pPrev
= ARENA_PTR( baseptr
, prev
);
213 LOCALARENA
*pNew
= ARENA_PTR( baseptr
, new );
215 pNew
->prev
= (prev
& ~3) | LOCAL_ARENA_FIXED
;
216 pNew
->next
= pPrev
->next
;
217 ARENA_PTR(baseptr
,pPrev
->next
)->prev
&= 3;
218 ARENA_PTR(baseptr
,pPrev
->next
)->prev
|= new;
223 /***********************************************************************
226 * Remove a block from the heap.
227 * 'block' is the handle of the block arena; 'baseptr' points to
228 * the beginning of the data segment containing the heap.
230 static void LOCAL_RemoveBlock( char *baseptr
, WORD block
)
232 LOCALARENA
*pArena
, *pTmp
;
234 /* Remove the block from the free-list */
236 TRACE(local
, "Local_RemoveBlock\n");
237 pArena
= ARENA_PTR( baseptr
, block
);
238 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
239 LOCAL_RemoveFreeBlock( baseptr
, block
);
241 /* If the previous block is free, expand its size */
243 pTmp
= ARENA_PTR( baseptr
, pArena
->prev
& ~3 );
244 if ((pTmp
->prev
& 3) == LOCAL_ARENA_FREE
)
245 pTmp
->size
+= pArena
->next
- block
;
247 /* Remove the block from the linked list */
249 pTmp
->next
= pArena
->next
;
250 pTmp
= ARENA_PTR( baseptr
, pArena
->next
);
251 pTmp
->prev
= (pTmp
->prev
& 3) | (pArena
->prev
& ~3);
255 /***********************************************************************
258 static void LOCAL_PrintHeap( HANDLE16 ds
)
261 LOCALHEAPINFO
*pInfo
;
264 /* FIXME - the test should be done when calling the function!
265 plus is not clear that we should print this info
266 only when TRACE_ON is on! */
267 if(!TRACE_ON(local
)) return;
269 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
270 pInfo
= LOCAL_GetHeap( ds
);
274 DUMP( "Local Heap corrupted! ds=%04x\n", ds
);
277 DUMP( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
278 ds
, pInfo
->first
, pInfo
->last
, pInfo
->items
);
280 arena
= pInfo
->first
;
283 LOCALARENA
*pArena
= ARENA_PTR(ptr
,arena
);
284 DUMP( " %04x: prev=%04x next=%04x type=%d\n", arena
,
285 pArena
->prev
& ~3, pArena
->next
, pArena
->prev
& 3 );
286 if (arena
== pInfo
->first
)
288 DUMP( " size=%d free_prev=%04x free_next=%04x\n",
289 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
291 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
293 DUMP( " size=%d free_prev=%04x free_next=%04x\n",
294 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
295 if (pArena
->next
== arena
) break; /* last one */
296 if (ARENA_PTR(ptr
,pArena
->free_next
)->free_prev
!= arena
)
298 DUMP( "*** arena->free_next->free_prev != arena\n" );
302 if (pArena
->next
== arena
)
304 DUMP( "*** last block is not marked free\n" );
307 if ((ARENA_PTR(ptr
,pArena
->next
)->prev
& ~3) != arena
)
309 DUMP( "*** arena->next->prev != arena (%04x, %04x)\n",
310 pArena
->next
, ARENA_PTR(ptr
,pArena
->next
)->prev
);
313 arena
= pArena
->next
;
318 /***********************************************************************
319 * LocalInit (KERNEL.4)
321 BOOL16 WINAPI
LocalInit( HANDLE16 selector
, WORD start
, WORD end
)
324 WORD heapInfoArena
, freeArena
, lastArena
;
325 LOCALHEAPINFO
*pHeapInfo
;
326 LOCALARENA
*pArena
, *pFirstArena
, *pLastArena
;
329 /* The initial layout of the heap is: */
330 /* - first arena (FIXED) */
331 /* - heap info structure (FIXED) */
332 /* - large free block (FREE) */
333 /* - last arena (FREE) */
335 TRACE(local
, "%04x %04x-%04x\n", selector
, start
, end
);
336 if (!selector
) selector
= CURRENT_DS
;
340 /* If TRACE_ON(heap) is set, the global heap blocks are */
341 /* cleared before use, so we can test for double initialization. */
342 if (LOCAL_GetHeap(selector
))
344 ERR(local
, "Heap %04x initialized twice.\n", selector
);
345 LOCAL_PrintHeap(selector
);
350 /* Check if the segment is the DGROUP of a module */
352 if ((pModule
= NE_GetPtr( selector
)))
354 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
) + pModule
->dgroup
- 1;
355 if (pModule
->dgroup
&& (GlobalHandleToSel(pSeg
->hSeg
) == selector
))
357 /* We can't just use the simple method of using the value
358 * of minsize + stacksize, since there are programs that
359 * resize the data segment before calling InitTask(). So,
360 * we must put it at the end of the segment */
361 start
= GlobalSize16( GlobalHandle16( selector
) );
364 TRACE(local
," new start %04x, minstart: %04x\n", start
, pSeg
->minsize
+ pModule
->stack_size
);
368 ptr
= PTR_SEG_OFF_TO_LIN( selector
, 0 );
370 start
= LALIGN( MAX( start
, sizeof(INSTANCEDATA
) ) );
371 heapInfoArena
= LALIGN(start
+ sizeof(LOCALARENA
) );
372 freeArena
= LALIGN( heapInfoArena
+ ARENA_HEADER_SIZE
373 + sizeof(LOCALHEAPINFO
) );
374 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
376 /* Make sure there's enough space. */
378 if (freeArena
+ sizeof(LOCALARENA
) >= lastArena
) return FALSE
;
380 /* Initialise the first arena */
382 pFirstArena
= ARENA_PTR( ptr
, start
);
383 pFirstArena
->prev
= start
| LOCAL_ARENA_FIXED
;
384 pFirstArena
->next
= heapInfoArena
;
385 pFirstArena
->size
= LALIGN(sizeof(LOCALARENA
));
386 pFirstArena
->free_prev
= start
; /* this one */
387 pFirstArena
->free_next
= freeArena
;
389 /* Initialise the arena of the heap info structure */
391 pArena
= ARENA_PTR( ptr
, heapInfoArena
);
392 pArena
->prev
= start
| LOCAL_ARENA_FIXED
;
393 pArena
->next
= freeArena
;
395 /* Initialise the heap info structure */
397 pHeapInfo
= (LOCALHEAPINFO
*) (ptr
+ heapInfoArena
+ ARENA_HEADER_SIZE
);
398 memset( pHeapInfo
, 0, sizeof(LOCALHEAPINFO
) );
399 pHeapInfo
->items
= 4;
400 pHeapInfo
->first
= start
;
401 pHeapInfo
->last
= lastArena
;
402 pHeapInfo
->htable
= 0;
403 pHeapInfo
->hdelta
= 0x20;
404 pHeapInfo
->extra
= 0x200;
405 pHeapInfo
->minsize
= lastArena
- freeArena
;
406 pHeapInfo
->magic
= LOCAL_HEAP_MAGIC
;
408 /* Initialise the large free block */
410 pArena
= ARENA_PTR( ptr
, freeArena
);
411 pArena
->prev
= heapInfoArena
| LOCAL_ARENA_FREE
;
412 pArena
->next
= lastArena
;
413 pArena
->size
= lastArena
- freeArena
;
414 pArena
->free_prev
= start
;
415 pArena
->free_next
= lastArena
;
417 /* Initialise the last block */
419 pLastArena
= ARENA_PTR( ptr
, lastArena
);
420 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
421 pLastArena
->next
= lastArena
; /* this one */
422 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
423 pLastArena
->free_prev
= freeArena
;
424 pLastArena
->free_next
= lastArena
; /* this one */
426 /* Store the local heap address in the instance data */
428 ((INSTANCEDATA
*)ptr
)->heap
= heapInfoArena
+ ARENA_HEADER_SIZE
;
429 LOCAL_PrintHeap( selector
);
433 /***********************************************************************
436 static BOOL16
LOCAL_GrowHeap( HANDLE16 ds
)
441 LOCALHEAPINFO
*pHeapInfo
;
442 WORD freeArena
, lastArena
;
443 LOCALARENA
*pArena
, *pLastArena
;
446 hseg
= GlobalHandle16( ds
);
447 /* maybe mem allocated by Virtual*() ? */
448 if (!hseg
) return FALSE
;
450 oldsize
= GlobalSize16( hseg
);
451 /* if nothing can be gained, return */
452 if (oldsize
> 0xfff0) return FALSE
;
453 hseg
= GlobalReAlloc16( hseg
, 0x10000, GMEM_FIXED
);
454 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
455 pHeapInfo
= LOCAL_GetHeap( ds
);
456 if (pHeapInfo
== NULL
) {
457 ERR(local
, "Heap not found\n" );
460 end
= GlobalSize16( hseg
);
461 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
463 /* Update the HeapInfo */
465 freeArena
= pHeapInfo
->last
;
466 pHeapInfo
->last
= lastArena
;
467 pHeapInfo
->minsize
+= end
- oldsize
;
469 /* grow the old last block */
470 pArena
= ARENA_PTR( ptr
, freeArena
);
471 pArena
->size
= lastArena
- freeArena
;
472 pArena
->next
= lastArena
;
473 pArena
->free_next
= lastArena
;
475 /* Initialise the new last block */
477 pLastArena
= ARENA_PTR( ptr
, lastArena
);
478 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
479 pLastArena
->next
= lastArena
; /* this one */
480 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
481 pLastArena
->free_prev
= freeArena
;
482 pLastArena
->free_next
= lastArena
; /* this one */
484 /* If block before freeArena is also free then merge them */
485 if((ARENA_PTR(ptr
, (pArena
->prev
& ~3))->prev
& 3) == LOCAL_ARENA_FREE
)
487 LOCAL_RemoveBlock(ptr
, freeArena
);
491 TRACE(local
, "Heap expanded\n" );
492 LOCAL_PrintHeap( ds
);
497 /***********************************************************************
500 static HLOCAL16
LOCAL_FreeArena( WORD ds
, WORD arena
)
502 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
503 LOCALHEAPINFO
*pInfo
;
504 LOCALARENA
*pArena
, *pPrev
, *pNext
;
506 TRACE(local
, "%04x ds=%04x\n", arena
, ds
);
507 if (!(pInfo
= LOCAL_GetHeap( ds
))) return arena
;
509 pArena
= ARENA_PTR( ptr
, arena
);
510 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
512 /* shouldn't happen */
513 ERR(local
, "Trying to free block %04x twice!\n",
515 LOCAL_PrintHeap( ds
);
519 /* Check if we can merge with the previous block */
521 pPrev
= ARENA_PTR( ptr
, pArena
->prev
& ~3 );
522 pNext
= ARENA_PTR( ptr
, pArena
->next
);
523 if ((pPrev
->prev
& 3) == LOCAL_ARENA_FREE
)
525 arena
= pArena
->prev
& ~3;
527 LOCAL_RemoveBlock( ptr
, pPrev
->next
);
530 else /* Make a new free block */
532 LOCAL_MakeBlockFree( ptr
, arena
);
535 /* Check if we can merge with the next block */
537 if ((pArena
->next
== pArena
->free_next
) &&
538 (pArena
->next
!= pInfo
->last
))
540 LOCAL_RemoveBlock( ptr
, pArena
->next
);
547 /***********************************************************************
550 * Shrink an arena by creating a free block at its end if possible.
551 * 'size' includes the arena header, and must be aligned.
553 static void LOCAL_ShrinkArena( WORD ds
, WORD arena
, WORD size
)
555 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
556 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
558 if (arena
+ size
+ LALIGN(sizeof(LOCALARENA
)) < pArena
->next
)
560 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
562 LOCAL_AddBlock( ptr
, arena
, arena
+ size
);
564 LOCAL_FreeArena( ds
, arena
+ size
);
569 /***********************************************************************
570 * LOCAL_GrowArenaDownward
572 * Grow an arena downward by using the previous arena (must be free).
574 static void LOCAL_GrowArenaDownward( WORD ds
, WORD arena
, WORD newsize
)
576 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
577 LOCALHEAPINFO
*pInfo
;
578 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
579 WORD prevArena
= pArena
->prev
& ~3;
580 LOCALARENA
*pPrevArena
= ARENA_PTR( ptr
, prevArena
);
584 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
585 offset
= pPrevArena
->size
;
586 size
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
587 LOCAL_RemoveFreeBlock( ptr
, prevArena
);
588 LOCAL_RemoveBlock( ptr
, arena
);
590 p
= (char *)pPrevArena
+ ARENA_HEADER_SIZE
;
591 while (offset
< size
)
593 memcpy( p
, p
+ offset
, offset
);
597 if (size
) memcpy( p
, p
+ offset
, size
);
598 LOCAL_ShrinkArena( ds
, prevArena
, newsize
);
603 /***********************************************************************
604 * LOCAL_GrowArenaUpward
606 * Grow an arena upward by using the next arena (must be free and big
607 * enough). Newsize includes the arena header and must be aligned.
609 static void LOCAL_GrowArenaUpward( WORD ds
, WORD arena
, WORD newsize
)
611 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
612 LOCALHEAPINFO
*pInfo
;
613 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
614 WORD nextArena
= pArena
->next
;
616 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
617 LOCAL_RemoveBlock( ptr
, nextArena
);
619 LOCAL_ShrinkArena( ds
, arena
, newsize
);
623 /***********************************************************************
626 static WORD
LOCAL_GetFreeSpace(WORD ds
, WORD countdiscard
)
628 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
629 LOCALHEAPINFO
*pInfo
;
634 if (!(pInfo
= LOCAL_GetHeap( ds
)))
636 ERR(local
, "Local heap not found\n" );
640 arena
= pInfo
->first
;
641 pArena
= ARENA_PTR( ptr
, arena
);
642 while (arena
!= pArena
->free_next
)
644 arena
= pArena
->free_next
;
645 pArena
= ARENA_PTR( ptr
, arena
);
646 if (pArena
->size
>= freespace
) freespace
= pArena
->size
;
648 /* FIXME doesn't yet calculate space that would become free if everything
649 were discarded when countdiscard == 1 */
650 if (freespace
< ARENA_HEADER_SIZE
) freespace
= 0;
651 else freespace
-= ARENA_HEADER_SIZE
;
656 /***********************************************************************
659 WORD
LOCAL_Compact( HANDLE16 ds
, UINT16 minfree
, UINT16 flags
)
661 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
662 LOCALHEAPINFO
*pInfo
;
663 LOCALARENA
*pArena
, *pMoveArena
, *pFinalArena
;
664 WORD arena
, movearena
, finalarena
, table
;
665 WORD count
, movesize
, size
;
667 LOCALHANDLEENTRY
*pEntry
;
669 if (!(pInfo
= LOCAL_GetHeap( ds
)))
671 ERR(local
, "Local heap not found\n" );
675 TRACE(local
, "ds = %04x, minfree = %04x, flags = %04x\n",
677 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
678 if(freespace
>= minfree
|| (flags
& LMEM_NOCOMPACT
))
680 TRACE(local
, "Returning %04x.\n", freespace
);
683 TRACE(local
, "Compacting heap %04x.\n", ds
);
684 table
= pInfo
->htable
;
687 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
688 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
690 if((pEntry
->lock
== 0) && (pEntry
->flags
!= (LMEM_DISCARDED
>> 8)))
692 /* OK we can move this one if we want */
693 TRACE(local
, "handle %04x (block %04x) can be moved.\n",
694 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
695 movearena
= ARENA_HEADER(pEntry
->addr
);
696 pMoveArena
= ARENA_PTR(ptr
, movearena
);
697 movesize
= pMoveArena
->next
- movearena
;
698 arena
= pInfo
->first
;
699 pArena
= ARENA_PTR(ptr
, arena
);
702 /* Try to find the smallest arena that will do, */
703 /* which is below us in memory */
706 arena
= pArena
->free_next
;
707 pArena
= ARENA_PTR(ptr
, arena
);
708 if(arena
>= movearena
)
710 if(arena
== pArena
->free_next
)
712 if((pArena
->size
>= movesize
) && (pArena
->size
< size
))
718 if (finalarena
) /* Actually got somewhere to move */
720 TRACE(local
, "Moving it to %04x.\n", finalarena
);
721 pFinalArena
= ARENA_PTR(ptr
, finalarena
);
722 size
= pFinalArena
->size
;
723 LOCAL_RemoveFreeBlock(ptr
, finalarena
);
724 LOCAL_ShrinkArena( ds
, finalarena
, movesize
);
725 /* Copy the arena to it's new location */
726 memcpy((char *)pFinalArena
+ ARENA_HEADER_SIZE
,
727 (char *)pMoveArena
+ ARENA_HEADER_SIZE
,
728 movesize
- ARENA_HEADER_SIZE
);
729 /* Free the old location */
730 LOCAL_FreeArena(ds
, movearena
);
732 Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_MOVE
,
733 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
734 /* Update handle table entry */
735 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ sizeof(HLOCAL16
) ;
737 else if((ARENA_PTR(ptr
, pMoveArena
->prev
& ~3)->prev
& 3)
740 /* Previous arena is free (but < movesize) */
741 /* so we can 'slide' movearena down into it */
742 finalarena
= pMoveArena
->prev
& ~3;
743 LOCAL_GrowArenaDownward( ds
, movearena
, movesize
);
744 /* Update handle table entry */
745 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ sizeof(HLOCAL16
) ;
749 table
= *(WORD
*)pEntry
;
751 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
752 if(freespace
>= minfree
|| (flags
& LMEM_NODISCARD
))
754 TRACE(local
, "Returning %04x.\n", freespace
);
758 table
= pInfo
->htable
;
761 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
762 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
764 if(pEntry
->addr
&& pEntry
->lock
== 0 &&
765 (pEntry
->flags
& (LMEM_DISCARDABLE
>> 8)))
767 TRACE(local
, "Discarding handle %04x (block %04x).\n",
768 (char *)pEntry
- ptr
, pEntry
->addr
);
769 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
));
771 Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_DISCARD
,
772 (char *)pEntry
- ptr
, pEntry
->flags
);
774 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
777 table
= *(WORD
*)pEntry
;
779 return LOCAL_Compact(ds
, 0xffff, LMEM_NODISCARD
);
783 /***********************************************************************
784 * LOCAL_FindFreeBlock
786 static HLOCAL16
LOCAL_FindFreeBlock( HANDLE16 ds
, WORD size
)
788 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
789 LOCALHEAPINFO
*pInfo
;
793 if (!(pInfo
= LOCAL_GetHeap( ds
)))
795 ERR(local
, "Local heap not found\n" );
800 arena
= pInfo
->first
;
801 pArena
= ARENA_PTR( ptr
, arena
);
803 arena
= pArena
->free_next
;
804 pArena
= ARENA_PTR( ptr
, arena
);
805 if (arena
== pArena
->free_next
) break;
806 if (pArena
->size
>= size
) return arena
;
808 TRACE(local
, "not enough space\n" );
813 /***********************************************************************
815 * The segment may get moved around in this function, so all callers
816 * should reset their pointer variables.
818 static HLOCAL16
LOCAL_GetBlock( HANDLE16 ds
, WORD size
, WORD flags
)
820 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
821 LOCALHEAPINFO
*pInfo
;
825 if (!(pInfo
= LOCAL_GetHeap( ds
)))
827 ERR(local
, "Local heap not found\n");
832 size
+= ARENA_HEADER_SIZE
;
833 size
= LALIGN( MAX( size
, sizeof(LOCALARENA
) ) );
838 /* Find a suitable free block */
839 arena
= LOCAL_FindFreeBlock( ds
, size
);
841 /* no space: try to make some */
842 LOCAL_Compact( ds
, size
, flags
);
843 arena
= LOCAL_FindFreeBlock( ds
, size
);
846 /* still no space: try to grow the segment */
847 if (!(LOCAL_GrowHeap( ds
)))
850 /* FIXME: doesn't work correctly yet */
851 if ((pInfo
->notify
) && (Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_OUTOFMEM
, ds
- 20, size
))) /* FIXME: "size" correct ? (should indicate bytes needed) */
856 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
857 pInfo
= LOCAL_GetHeap( ds
);
858 arena
= LOCAL_FindFreeBlock( ds
, size
);
861 if (ds
== GDI_HeapSel
) {
862 ERR(local
, "not enough space in GDI local heap "
863 "(%04x) for %d bytes\n", ds
, size
);
864 } else if (ds
== USER_HeapSel
) {
865 ERR(local
, "not enough space in USER local heap "
866 "(%04x) for %d bytes\n", ds
, size
);
868 ERR(local
, "not enough space in local heap "
869 "%04x for %d bytes\n", ds
, size
);
872 if ((pInfo
->notify
) &&
873 /* FIXME: "size" correct ? (should indicate bytes needed) */
874 (Callbacks
->CallLocalNotifyFunc(pInfo
->notify
, LN_OUTOFMEM
, ds
, size
)))
880 /* Make a block out of the free arena */
881 pArena
= ARENA_PTR( ptr
, arena
);
882 TRACE(local
, "LOCAL_GetBlock size = %04x, arena %04x size %04x\n",
883 size
, arena
, pArena
->size
);
884 LOCAL_RemoveFreeBlock( ptr
, arena
);
885 LOCAL_ShrinkArena( ds
, arena
, size
);
887 if (flags
& LMEM_ZEROINIT
)
888 memset((char *)pArena
+ ARENA_HEADER_SIZE
, 0, size
-ARENA_HEADER_SIZE
);
889 return arena
+ ARENA_HEADER_SIZE
;
893 /***********************************************************************
896 static BOOL16
LOCAL_NewHTable( HANDLE16 ds
)
898 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
899 LOCALHEAPINFO
*pInfo
;
900 LOCALHANDLEENTRY
*pEntry
;
904 TRACE(local
, "Local_NewHTable\n" );
905 if (!(pInfo
= LOCAL_GetHeap( ds
)))
907 ERR(local
, "Local heap not found\n");
912 if (!(handle
= LOCAL_GetBlock( ds
, pInfo
->hdelta
* sizeof(LOCALHANDLEENTRY
)
913 + 2 * sizeof(WORD
), LMEM_FIXED
)))
915 if (!(ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 )))
916 ERR(local
, "ptr == NULL after GetBlock.\n");
917 if (!(pInfo
= LOCAL_GetHeap( ds
)))
918 ERR(local
,"pInfo == NULL after GetBlock.\n");
920 /* Fill the entry table */
922 *(WORD
*)(ptr
+ handle
) = pInfo
->hdelta
;
923 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
+ sizeof(WORD
));
924 for (i
= pInfo
->hdelta
; i
> 0; i
--, pEntry
++) {
925 pEntry
->lock
= pEntry
->flags
= 0xff;
928 *(WORD
*)pEntry
= pInfo
->htable
;
929 pInfo
->htable
= handle
;
934 /***********************************************************************
935 * LOCAL_GetNewHandleEntry
937 static HLOCAL16
LOCAL_GetNewHandleEntry( HANDLE16 ds
)
939 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
940 LOCALHEAPINFO
*pInfo
;
941 LOCALHANDLEENTRY
*pEntry
= NULL
;
944 if (!(pInfo
= LOCAL_GetHeap( ds
)))
946 ERR(local
, "Local heap not found\n");
951 /* Find a free slot in existing tables */
953 table
= pInfo
->htable
;
956 WORD count
= *(WORD
*)(ptr
+ table
);
957 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
958 for (; count
> 0; count
--, pEntry
++)
959 if (pEntry
->lock
== 0xff) break;
961 table
= *(WORD
*)pEntry
;
964 if (!table
) /* We need to create a new table */
966 if (!LOCAL_NewHTable( ds
)) return 0;
967 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
968 pInfo
= LOCAL_GetHeap( ds
);
969 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ pInfo
->htable
+ sizeof(WORD
));
972 /* Now allocate this entry */
976 TRACE(local
, "(%04x): %04x\n",
977 ds
, ((char *)pEntry
- ptr
) );
978 return (HLOCAL16
)((char *)pEntry
- ptr
);
982 /***********************************************************************
983 * LOCAL_FreeHandleEntry
985 * Free a handle table entry.
987 static void LOCAL_FreeHandleEntry( HANDLE16 ds
, HLOCAL16 handle
)
989 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
990 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
991 LOCALHEAPINFO
*pInfo
;
993 WORD table
, count
, i
;
995 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
997 /* Find the table where this handle comes from */
999 pTable
= &pInfo
->htable
;
1002 WORD size
= (*(WORD
*)(ptr
+ *pTable
)) * sizeof(LOCALHANDLEENTRY
);
1003 if ((handle
>= *pTable
+ sizeof(WORD
)) &&
1004 (handle
< *pTable
+ sizeof(WORD
) + size
)) break; /* Found it */
1005 pTable
= (WORD
*)(ptr
+ *pTable
+ sizeof(WORD
) + size
);
1009 ERR(local
, "Invalid entry %04x\n", handle
);
1010 LOCAL_PrintHeap( ds
);
1014 /* Make the entry free */
1016 pEntry
->addr
= 0; /* just in case */
1017 pEntry
->lock
= 0xff;
1018 pEntry
->flags
= 0xff;
1019 /* Now check if all entries in this table are free */
1022 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1023 count
= *(WORD
*)(ptr
+ table
);
1024 for (i
= count
; i
> 0; i
--, pEntry
++) if (pEntry
->lock
!= 0xff) return;
1026 /* Remove the table from the linked list and free it */
1028 TRACE(local
, "(%04x): freeing table %04x\n",
1030 *pTable
= *(WORD
*)pEntry
;
1031 LOCAL_FreeArena( ds
, ARENA_HEADER( table
) );
1035 /***********************************************************************
1038 * Implementation of LocalFree().
1040 HLOCAL16
LOCAL_Free( HANDLE16 ds
, HLOCAL16 handle
)
1042 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1044 TRACE(local
, "%04x ds=%04x\n", handle
, ds
);
1046 if (!handle
) { WARN(local
, "Handle is 0.\n" ); return 0; }
1047 if (HANDLE_FIXED( handle
))
1049 if (!LOCAL_FreeArena( ds
, ARENA_HEADER( handle
) )) return 0; /* OK */
1050 else return handle
; /* couldn't free it */
1054 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1055 if (pEntry
->flags
!= (LMEM_DISCARDED
>> 8))
1057 TRACE(local
, "real block at %04x\n",
1059 if (LOCAL_FreeArena( ds
, ARENA_HEADER(pEntry
->addr
) ))
1060 return handle
; /* couldn't free it */
1062 LOCAL_FreeHandleEntry( ds
, handle
);
1068 /***********************************************************************
1071 * Implementation of LocalAlloc().
1074 HLOCAL16
LOCAL_Alloc( HANDLE16 ds
, WORD flags
, WORD size
)
1079 TRACE(local
, "%04x %d ds=%04x\n", flags
, size
, ds
);
1081 if(size
> 0 && size
<= 4) size
= 5;
1082 if (flags
& LMEM_MOVEABLE
)
1084 LOCALHANDLEENTRY
*plhe
;
1089 if (!(hmem
= LOCAL_GetBlock( ds
, size
+ sizeof(HLOCAL16
), flags
)))
1092 else /* We just need to allocate a discarded handle */
1094 if (!(handle
= LOCAL_GetNewHandleEntry( ds
)))
1096 WARN(local
, "Couldn't get handle.\n");
1098 LOCAL_FreeArena( ds
, ARENA_HEADER(hmem
) );
1101 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1102 plhe
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1106 plhe
->addr
= hmem
+ sizeof(HLOCAL16
);
1107 plhe
->flags
= (BYTE
)((flags
& 0x0f00) >> 8);
1108 *(HLOCAL16
*)(ptr
+ hmem
) = handle
;
1113 plhe
->flags
= LMEM_DISCARDED
>> 8;
1120 handle
= LOCAL_GetBlock( ds
, size
, flags
);
1126 /***********************************************************************
1129 * Implementation of LocalReAlloc().
1131 HLOCAL16
LOCAL_ReAlloc( HANDLE16 ds
, HLOCAL16 handle
, WORD size
, WORD flags
)
1133 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1134 LOCALHEAPINFO
*pInfo
;
1135 LOCALARENA
*pArena
, *pNext
;
1136 LOCALHANDLEENTRY
*pEntry
= NULL
;
1137 WORD arena
, oldsize
;
1138 HLOCAL16 hmem
, blockhandle
;
1141 if (!handle
) return 0;
1142 if(HANDLE_MOVEABLE(handle
) &&
1143 ((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
== 0xff) /* An unused handle */
1146 TRACE(local
, "%04x %d %04x ds=%04x\n",
1147 handle
, size
, flags
, ds
);
1148 if (!(pInfo
= LOCAL_GetHeap( ds
))) return 0;
1150 if (HANDLE_FIXED( handle
))
1151 blockhandle
= handle
;
1154 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1155 if(pEntry
->flags
== (LMEM_DISCARDED
>> 8))
1159 WARN(local
,"Dicarded block has non-zero addr.\n");
1160 TRACE(local
, "ReAllocating discarded block\n");
1161 if(size
<= 4) size
= 5;
1162 if (!(hl
= LOCAL_GetBlock( ds
, size
+ sizeof(HLOCAL16
), flags
)))
1164 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 ); /* Reload ptr */
1165 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1166 pEntry
->addr
= hl
+ sizeof(HLOCAL16
);
1169 *(HLOCAL16
*)(ptr
+ hl
) = handle
;
1172 if (((blockhandle
= pEntry
->addr
) & 3) != 2)
1174 ERR(local
, "(%04x,%04x): invalid handle\n",
1178 if(*((HLOCAL16
*)(ptr
+ blockhandle
) - 1) != handle
) {
1179 ERR(local
, "Back ptr to handle is invalid\n");
1184 if (flags
& LMEM_MODIFY
)
1186 if (HANDLE_MOVEABLE(handle
))
1188 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1189 pEntry
->flags
= (flags
& 0x0f00) >> 8;
1190 TRACE(local
, "Changing flags to %x.\n", pEntry
->flags
);
1197 if (flags
& LMEM_MOVEABLE
)
1199 if (HANDLE_FIXED(handle
))
1201 TRACE(local
, "Freeing fixed block.\n");
1202 return LOCAL_Free( ds
, handle
);
1204 else /* Moveable block */
1206 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1207 if (pEntry
->lock
== 0)
1209 /* discards moveable blocks */
1210 TRACE(local
,"Discarding block\n");
1211 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
));
1213 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
1221 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1222 if (pEntry
->lock
== 0)
1225 return LOCAL_Free( ds
, handle
);
1231 arena
= ARENA_HEADER( blockhandle
);
1232 TRACE(local
, "arena is %04x\n", arena
);
1233 pArena
= ARENA_PTR( ptr
, arena
);
1235 if(size
<= 4) size
= 5;
1236 oldsize
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
1237 nextarena
= LALIGN(blockhandle
+ size
);
1239 /* Check for size reduction */
1241 if (nextarena
<= pArena
->next
)
1243 TRACE(local
, "size reduction, making new free block\n");
1244 LOCAL_ShrinkArena(ds
, arena
, nextarena
- arena
);
1245 TRACE(local
, "returning %04x\n", handle
);
1249 /* Check if the next block is free and large enough */
1251 pNext
= ARENA_PTR( ptr
, pArena
->next
);
1252 if (((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) &&
1253 (nextarena
<= pNext
->next
))
1255 TRACE(local
, "size increase, making new free block\n");
1256 LOCAL_GrowArenaUpward(ds
, arena
, nextarena
- arena
);
1257 TRACE(local
, "returning %04x\n", handle
);
1261 /* Now we have to allocate a new block, but not if (fixed block or locked
1262 block) and no LMEM_MOVEABLE */
1264 if (!(flags
& LMEM_MOVEABLE
))
1266 if (HANDLE_FIXED(handle
))
1268 ERR(local
, "Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1273 if(((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
!= 0)
1275 ERR(local
, "Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1280 if(HANDLE_MOVEABLE(handle
)) size
+= sizeof(HLOCAL16
);
1281 hmem
= LOCAL_GetBlock( ds
, size
, flags
);
1282 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 ); /* Reload ptr */
1283 if(HANDLE_MOVEABLE(handle
)) /* LOCAL_GetBlock might have triggered */
1284 { /* a compaction, which might in turn have */
1285 blockhandle
= pEntry
->addr
; /* moved the very block we are resizing */
1286 arena
= ARENA_HEADER( blockhandle
); /* thus, we reload arena, too */
1290 /* Remove the block from the heap and try again */
1291 LPSTR buffer
= HeapAlloc( GetProcessHeap(), 0, oldsize
);
1292 if (!buffer
) return 0;
1293 memcpy( buffer
, ptr
+ arena
+ ARENA_HEADER_SIZE
, oldsize
);
1294 LOCAL_FreeArena( ds
, arena
);
1295 if (!(hmem
= LOCAL_GetBlock( ds
, size
, flags
)))
1297 if (!(hmem
= LOCAL_GetBlock( ds
, oldsize
, flags
)))
1299 ERR(local
, "Can't restore saved block\n" );
1300 HeapFree( GetProcessHeap(), 0, buffer
);
1305 ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 ); /* Reload ptr */
1306 memcpy( ptr
+ hmem
, buffer
, oldsize
);
1307 HeapFree( GetProcessHeap(), 0, buffer
);
1311 memcpy( ptr
+ hmem
, ptr
+ (arena
+ ARENA_HEADER_SIZE
), oldsize
);
1312 LOCAL_FreeArena( ds
, arena
);
1314 if (HANDLE_MOVEABLE( handle
))
1316 TRACE(local
, "fixing handle\n");
1317 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1318 pEntry
->addr
= hmem
+ sizeof(HLOCAL16
);
1319 /* Back ptr should still be correct */
1320 if(*(HLOCAL16
*)(ptr
+ hmem
) != handle
)
1321 ERR(local
, "back ptr is invalid.\n");
1324 if (size
== oldsize
) hmem
= 0; /* Realloc failed */
1325 TRACE(local
, "returning %04x\n", hmem
);
1330 /***********************************************************************
1331 * LOCAL_InternalLock
1333 static HLOCAL16
LOCAL_InternalLock( LPSTR heap
, HLOCAL16 handle
)
1335 HLOCAL16 old_handle
= handle
;
1337 if (HANDLE_MOVEABLE(handle
))
1339 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(heap
+ handle
);
1340 if (pEntry
->flags
== LMEM_DISCARDED
) return 0;
1341 if (pEntry
->lock
< 0xfe) pEntry
->lock
++;
1342 handle
= pEntry
->addr
;
1344 TRACE(local
, "%04x returning %04x\n",
1345 old_handle
, handle
);
1350 /***********************************************************************
1353 LPSTR
LOCAL_Lock( HANDLE16 ds
, HLOCAL16 handle
)
1355 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1356 return handle
? ptr
+ LOCAL_InternalLock( ptr
, handle
) : NULL
;
1360 /***********************************************************************
1363 SEGPTR
LOCAL_LockSegptr( HANDLE16 ds
, HLOCAL16 handle
)
1365 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1366 return PTR_SEG_OFF_TO_SEGPTR( ds
, LOCAL_InternalLock( ptr
, handle
) );
1370 /***********************************************************************
1373 BOOL16
LOCAL_Unlock( HANDLE16 ds
, HLOCAL16 handle
)
1375 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1377 TRACE(local
, "%04x\n", handle
);
1378 if (HANDLE_MOVEABLE(handle
))
1380 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1381 if (!pEntry
->lock
|| (pEntry
->lock
== 0xff)) return FALSE
;
1382 /* For moveable block, return the new lock count */
1383 /* (see _Windows_Internals_ p. 197) */
1384 return --pEntry
->lock
;
1390 /***********************************************************************
1393 * Implementation of LocalSize().
1395 WORD
LOCAL_Size( HANDLE16 ds
, HLOCAL16 handle
)
1397 char *ptr
= PTR_SEG_OFF_TO_LIN( CURRENT_DS
, 0 );
1400 TRACE(local
, "%04x ds=%04x\n", handle
, ds
);
1402 if (HANDLE_MOVEABLE( handle
)) handle
= *(WORD
*)(ptr
+ handle
);
1403 if (!handle
) return 0;
1404 pArena
= ARENA_PTR( ptr
, ARENA_HEADER(handle
) );
1405 return pArena
->next
- handle
;
1409 /***********************************************************************
1412 * Implementation of LocalFlags().
1414 WORD
LOCAL_Flags( HANDLE16 ds
, HLOCAL16 handle
)
1416 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1418 if (HANDLE_MOVEABLE(handle
))
1420 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1421 TRACE(local
, "(%04x,%04x): returning %04x\n",
1422 ds
, handle
, pEntry
->lock
| (pEntry
->flags
<< 8) );
1423 return pEntry
->lock
| (pEntry
->flags
<< 8);
1427 TRACE(local
, "(%04x,%04x): returning 0\n",
1434 /***********************************************************************
1437 * Implementation of LocalHeapSize().
1439 WORD
LOCAL_HeapSize( HANDLE16 ds
)
1441 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1442 if (!pInfo
) return 0;
1443 return pInfo
->last
- pInfo
->first
;
1447 /***********************************************************************
1450 * Implementation of LocalCountFree().
1452 WORD
LOCAL_CountFree( HANDLE16 ds
)
1456 LOCALHEAPINFO
*pInfo
;
1457 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1459 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1461 ERR(local
, "(%04x): Local heap not found\n", ds
);
1462 LOCAL_PrintHeap( ds
);
1467 arena
= pInfo
->first
;
1468 pArena
= ARENA_PTR( ptr
, arena
);
1471 arena
= pArena
->free_next
;
1472 pArena
= ARENA_PTR( ptr
, arena
);
1473 if (arena
== pArena
->free_next
) break;
1474 total
+= pArena
->size
;
1476 TRACE(local
, "(%04x): returning %d\n", ds
, total
);
1481 /***********************************************************************
1484 * Implementation of LocalHandle().
1486 HLOCAL16
LOCAL_Handle( HANDLE16 ds
, WORD addr
)
1488 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1489 LOCALHEAPINFO
*pInfo
;
1492 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1494 ERR(local
, "(%04x): Local heap not found\n", ds
);
1495 LOCAL_PrintHeap( ds
);
1499 /* Find the address in the entry tables */
1501 table
= pInfo
->htable
;
1504 WORD count
= *(WORD
*)(ptr
+ table
);
1505 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+table
+sizeof(WORD
));
1506 for (; count
> 0; count
--, pEntry
++)
1507 if (pEntry
->addr
== addr
) return (HLOCAL16
)((char *)pEntry
- ptr
);
1508 table
= *(WORD
*)pEntry
;
1511 return (HLOCAL16
)addr
; /* Fixed block handle is addr */
1515 /***********************************************************************
1516 * LocalAlloc16 (KERNEL.5)
1518 HLOCAL16 WINAPI
LocalAlloc16( UINT16 flags
, WORD size
)
1520 return LOCAL_Alloc( CURRENT_DS
, flags
, size
);
1524 /***********************************************************************
1525 * LocalReAlloc16 (KERNEL.6)
1527 HLOCAL16 WINAPI
LocalReAlloc16( HLOCAL16 handle
, WORD size
, UINT16 flags
)
1529 return LOCAL_ReAlloc( CURRENT_DS
, handle
, size
, flags
);
1533 /***********************************************************************
1534 * LocalFree16 (KERNEL.7)
1536 HLOCAL16 WINAPI
LocalFree16( HLOCAL16 handle
)
1538 return LOCAL_Free( CURRENT_DS
, handle
);
1542 /***********************************************************************
1543 * LocalLock16 (KERNEL.8)
1545 * Note: only the offset part of the pointer is returned by the relay code.
1547 SEGPTR WINAPI
LocalLock16( HLOCAL16 handle
)
1549 return LOCAL_LockSegptr( CURRENT_DS
, handle
);
1553 /***********************************************************************
1554 * LocalUnlock16 (KERNEL.9)
1556 BOOL16 WINAPI
LocalUnlock16( HLOCAL16 handle
)
1558 return LOCAL_Unlock( CURRENT_DS
, handle
);
1562 /***********************************************************************
1563 * LocalSize16 (KERNEL.10)
1565 UINT16 WINAPI
LocalSize16( HLOCAL16 handle
)
1567 return LOCAL_Size( CURRENT_DS
, handle
);
1571 /***********************************************************************
1572 * LocalHandle16 (KERNEL.11)
1574 HLOCAL16 WINAPI
LocalHandle16( WORD addr
)
1576 return LOCAL_Handle( CURRENT_DS
, addr
);
1580 /***********************************************************************
1581 * LocalFlags16 (KERNEL.12)
1583 UINT16 WINAPI
LocalFlags16( HLOCAL16 handle
)
1585 return LOCAL_Flags( CURRENT_DS
, handle
);
1589 /***********************************************************************
1590 * LocalCompact16 (KERNEL.13)
1592 UINT16 WINAPI
LocalCompact16( UINT16 minfree
)
1594 TRACE(local
, "%04x\n", minfree
);
1595 return LOCAL_Compact( CURRENT_DS
, minfree
, 0 );
1599 /***********************************************************************
1600 * LocalNotify (KERNEL.14)
1602 * Installs a callback function that is called for local memory events
1603 * Callback function prototype is
1604 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1607 * NotifyFunc seems to be responsible for allocating some memory,
1608 * returns TRUE for success.
1609 * wArg = number of bytes needed additionally
1611 * hMem = handle; wArg = old mem location
1613 * NotifyFunc seems to be strongly encouraged to return TRUE,
1614 * otherwise LogError() gets called.
1615 * hMem = handle; wArg = flags
1617 FARPROC16 WINAPI
LocalNotify( FARPROC16 func
)
1619 LOCALHEAPINFO
*pInfo
;
1620 FARPROC16 oldNotify
;
1621 HANDLE16 ds
= CURRENT_DS
;
1623 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1625 ERR(local
, "(%04x): Local heap not found\n", ds
);
1626 LOCAL_PrintHeap( ds
);
1629 TRACE(local
, "(%04x): %08lx\n", ds
, (DWORD
)func
);
1630 FIXME(local
, "Half implemented\n");
1631 oldNotify
= pInfo
->notify
;
1632 pInfo
->notify
= func
;
1637 /***********************************************************************
1638 * LocalShrink16 (KERNEL.121)
1640 UINT16 WINAPI
LocalShrink16( HGLOBAL16 handle
, UINT16 newsize
)
1642 TRACE(local
, "%04x %04x\n", handle
, newsize
);
1647 /***********************************************************************
1648 * GetHeapSpaces (KERNEL.138)
1650 DWORD WINAPI
GetHeapSpaces( HMODULE16 module
)
1655 if (!(pModule
= NE_GetPtr( module
))) return 0;
1657 GlobalHandleToSel((NE_SEG_TABLE( pModule
) + pModule
->dgroup
- 1)->hSeg
);
1658 return MAKELONG( LOCAL_CountFree( ds
), LOCAL_HeapSize( ds
) );
1662 /***********************************************************************
1663 * LocalCountFree (KERNEL.161)
1665 WORD WINAPI
LocalCountFree(void)
1667 return LOCAL_CountFree( CURRENT_DS
);
1671 /***********************************************************************
1672 * LocalHeapSize (KERNEL.162)
1674 WORD WINAPI
LocalHeapSize(void)
1676 TRACE(local
, "(void)\n" );
1677 return LOCAL_HeapSize( CURRENT_DS
);
1681 /***********************************************************************
1682 * LocalHandleDelta (KERNEL.310)
1684 WORD WINAPI
LocalHandleDelta( WORD delta
)
1686 LOCALHEAPINFO
*pInfo
;
1688 if (!(pInfo
= LOCAL_GetHeap( CURRENT_DS
)))
1690 ERR(local
, "Local heap not found\n");
1691 LOCAL_PrintHeap( CURRENT_DS
);
1694 if (delta
) pInfo
->hdelta
= delta
;
1695 TRACE(local
, "returning %04x\n", pInfo
->hdelta
);
1696 return pInfo
->hdelta
;
1700 /***********************************************************************
1701 * LocalInfo (TOOLHELP.56)
1703 BOOL16 WINAPI
LocalInfo( LOCALINFO
*pLocalInfo
, HGLOBAL16 handle
)
1705 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap(SELECTOROF(WIN16_GlobalLock16(handle
)));
1706 if (!pInfo
) return FALSE
;
1707 pLocalInfo
->wcItems
= pInfo
->items
;
1712 /***********************************************************************
1713 * LocalFirst (TOOLHELP.57)
1715 BOOL16 WINAPI
LocalFirst( LOCALENTRY
*pLocalEntry
, HGLOBAL16 handle
)
1717 WORD ds
= GlobalHandleToSel( handle
);
1718 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1719 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1720 if (!pInfo
) return FALSE
;
1722 pLocalEntry
->hHandle
= pInfo
->first
+ ARENA_HEADER_SIZE
;
1723 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1724 pLocalEntry
->wFlags
= LF_FIXED
;
1725 pLocalEntry
->wcLock
= 0;
1726 pLocalEntry
->wType
= LT_NORMAL
;
1727 pLocalEntry
->hHeap
= handle
;
1728 pLocalEntry
->wHeapType
= NORMAL_HEAP
;
1729 pLocalEntry
->wNext
= ARENA_PTR(ptr
,pInfo
->first
)->next
;
1730 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1735 /***********************************************************************
1736 * LocalNext (TOOLHELP.58)
1738 BOOL16 WINAPI
LocalNext( LOCALENTRY
*pLocalEntry
)
1740 WORD ds
= GlobalHandleToSel( pLocalEntry
->hHeap
);
1741 char *ptr
= PTR_SEG_OFF_TO_LIN( ds
, 0 );
1744 if (!LOCAL_GetHeap( ds
)) return FALSE
;
1745 if (!pLocalEntry
->wNext
) return FALSE
;
1746 pArena
= ARENA_PTR( ptr
, pLocalEntry
->wNext
);
1748 pLocalEntry
->hHandle
= pLocalEntry
->wNext
+ ARENA_HEADER_SIZE
;
1749 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1750 pLocalEntry
->wFlags
= (pArena
->prev
& 3) + 1;
1751 pLocalEntry
->wcLock
= 0;
1752 pLocalEntry
->wType
= LT_NORMAL
;
1753 if (pArena
->next
!= pLocalEntry
->wNext
) /* last one? */
1754 pLocalEntry
->wNext
= pArena
->next
;
1756 pLocalEntry
->wNext
= 0;
1757 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1762 /***********************************************************************
1763 * LocalAlloc32 (KERNEL32.371)
1768 HLOCAL32 WINAPI
LocalAlloc32(
1769 UINT32 flags
, /* [in] Allocation attributes */
1770 DWORD size
/* [in] Number of bytes to allocate */
1772 return (HLOCAL32
)GlobalAlloc32( flags
, size
);
1776 /***********************************************************************
1777 * LocalCompact32 (KERNEL32.372)
1779 UINT32 WINAPI
LocalCompact32( UINT32 minfree
)
1781 return 0; /* LocalCompact does nothing in Win32 */
1785 /***********************************************************************
1786 * LocalFlags32 (KERNEL32.374)
1788 * Value specifying allocation flags and lock count.
1789 * LMEM_INVALID_HANDLE: Failure
1791 UINT32 WINAPI
LocalFlags32(
1792 HLOCAL32 handle
/* [in] Handle of memory object */
1794 return GlobalFlags32( (HGLOBAL32
)handle
);
1798 /***********************************************************************
1799 * LocalFree32 (KERNEL32.375)
1804 HLOCAL32 WINAPI
LocalFree32(
1805 HLOCAL32 handle
/* [in] Handle of memory object */
1807 return (HLOCAL32
)GlobalFree32( (HGLOBAL32
)handle
);
1811 /***********************************************************************
1812 * LocalHandle32 (KERNEL32.376)
1817 HLOCAL32 WINAPI
LocalHandle32(
1818 LPCVOID ptr
/* [in] Address of local memory object */
1820 return (HLOCAL32
)GlobalHandle32( ptr
);
1824 /***********************************************************************
1825 * LocalLock32 (KERNEL32.377)
1826 * Locks a local memory object and returns pointer to the first byte
1827 * of the memory block.
1833 LPVOID WINAPI
LocalLock32(
1834 HLOCAL32 handle
/* [in] Address of local memory object */
1836 return GlobalLock32( (HGLOBAL32
)handle
);
1840 /***********************************************************************
1841 * LocalReAlloc32 (KERNEL32.378)
1846 HLOCAL32 WINAPI
LocalReAlloc32(
1847 HLOCAL32 handle
, /* [in] Handle of memory object */
1848 DWORD size
, /* [in] New size of block */
1849 UINT32 flags
/* [in] How to reallocate object */
1851 return (HLOCAL32
)GlobalReAlloc32( (HGLOBAL32
)handle
, size
, flags
);
1855 /***********************************************************************
1856 * LocalShrink32 (KERNEL32.379)
1858 UINT32 WINAPI
LocalShrink32( HGLOBAL32 handle
, UINT32 newsize
)
1860 return 0; /* LocalShrink does nothing in Win32 */
1864 /***********************************************************************
1865 * LocalSize32 (KERNEL32.380)
1870 UINT32 WINAPI
LocalSize32(
1871 HLOCAL32 handle
/* [in] Handle of memory object */
1873 return GlobalSize32( (HGLOBAL32
)handle
);
1877 /***********************************************************************
1878 * LocalUnlock32 (KERNEL32.381)
1880 * TRUE: Object is still locked
1881 * FALSE: Object is unlocked
1883 BOOL32 WINAPI
LocalUnlock32(
1884 HLOCAL32 handle
/* [in] Handle of memory object */
1886 return GlobalUnlock32( (HGLOBAL32
)handle
);