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"
32 WORD prev; /* Previous arena | arena type */
33 WORD next; /* Next arena */
34 /* Start of the memory block or free-list info */
35 WORD size; /* Size of the free block */
36 WORD free_prev; /* Previous free block */
37 WORD free_next; /* Next free block */
40 #define ARENA_HEADER_SIZE 4
41 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
43 /* Arena types (stored in 'prev' field of the arena) */
44 #define LOCAL_ARENA_FREE 0
45 #define LOCAL_ARENA_FIXED 1
47 /* Layout of a handle entry table
49 * WORD count of entries
50 * LOCALHANDLEENTRY[count] entries
51 * WORD near ptr to next table
55 WORD addr; /* Address of the MOVEABLE block */
56 BYTE flags; /* Flags for this block */
57 BYTE lock; /* Lock count */
61 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
62 * in case something actually relies on this.
63 * Note the ARENA_HEADER(addr) still produces the desired result ie. 4n - 4
65 * An unused handle has lock = flags = 0xff. In windows addr is that of next
66 * free handle, at the moment in wine we set it to 0.
68 * A discarded block's handle has lock = addr = 0 and flags = 0x40
69 * (LMEM_DISCARDED >> 8)
76 WORD check; /* 00 Heap checking flag */
77 WORD freeze; /* 02 Heap frozen flag */
78 WORD items; /* 04 Count of items on the heap */
79 WORD first; /* 06 First item of the heap */
80 WORD pad1; /* 08 Always 0 */
81 WORD last; /* 0a Last item of the heap */
82 WORD pad2; /* 0c Always 0 */
83 BYTE ncompact; /* 0e Compactions counter */
84 BYTE dislevel; /* 0f Discard level */
85 DWORD distotal; /* 10 Total bytes discarded */
86 WORD htable; /* 14 Pointer to handle table */
87 WORD hfree; /* 16 Pointer to free handle table */
88 WORD hdelta; /* 18 Delta to expand the handle table */
89 WORD expand; /* 1a Pointer to expand function (unused) */
90 WORD pstat; /* 1c Pointer to status structure (unused) */
91 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
92 WORD lock; /* 22 Lock count for the heap */
93 WORD extra; /* 24 Extra bytes to allocate when expanding */
94 WORD minsize; /* 26 Minimum size of the heap */
95 WORD magic; /* 28 Magic number */
100 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
102 WORD USER_HeapSel = 0; /* USER heap selector */
103 WORD GDI_HeapSel = 0; /* GDI heap selector */
105 /* All local heap allocations are aligned on 4-byte boundaries */
106 #define LALIGN(word) (((word) + 3) & ~3)
108 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
109 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
110 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
111 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
113 /* determine whether the handle belongs to a fixed or a moveable block */
114 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
115 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
117 /***********************************************************************
120 * Return a pointer to the local heap, making sure it exists.
122 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
124 LOCALHEAPINFO *pInfo;
125 INSTANCEDATA *ptr = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( ds, 0 );
126 TRACE(local, "Heap at %p, %04x\n", ptr, ptr->heap );
127 if (!ptr || !ptr->heap) return NULL;
128 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
130 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
131 if (pInfo->magic != LOCAL_HEAP_MAGIC) return NULL;
136 /***********************************************************************
137 * LOCAL_MakeBlockFree
139 * Make a block free, inserting it in the free-list.
140 * 'block' is the handle of the block arena; 'baseptr' points to
141 * the beginning of the data segment containing the heap.
143 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
145 LOCALARENA *pArena, *pNext;
148 /* Mark the block as free */
150 pArena = ARENA_PTR( baseptr, block );
151 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
152 pArena->size = pArena->next - block;
154 /* Find the next free block (last block is always free) */
159 pNext = ARENA_PTR( baseptr, next );
160 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
164 TRACE(local, "Local_MakeBlockFree %04x, next %04x\n", block, next );
165 /* Insert the free block in the free-list */
167 pArena->free_prev = pNext->free_prev;
168 pArena->free_next = next;
169 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
170 pNext->free_prev = block;
174 /***********************************************************************
175 * LOCAL_RemoveFreeBlock
177 * Remove a block from the free-list.
178 * 'block' is the handle of the block arena; 'baseptr' points to
179 * the beginning of the data segment containing the heap.
181 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
183 /* Mark the block as fixed */
185 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
186 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
188 /* Remove it from the list */
190 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
191 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
195 /***********************************************************************
198 * Insert a new block in the heap.
199 * 'new' is the handle of the new block arena; 'baseptr' points to
200 * the beginning of the data segment containing the heap; 'prev' is
201 * the block before the new one.
203 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
205 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
206 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
208 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
209 pNew->next = pPrev->next;
210 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
211 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
216 /***********************************************************************
219 * Remove a block from the heap.
220 * 'block' is the handle of the block arena; 'baseptr' points to
221 * the beginning of the data segment containing the heap.
223 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
225 LOCALARENA *pArena, *pTmp;
227 /* Remove the block from the free-list */
229 TRACE(local, "Local_RemoveBlock\n");
230 pArena = ARENA_PTR( baseptr, block );
231 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
232 LOCAL_RemoveFreeBlock( baseptr, block );
234 /* If the previous block is free, expand its size */
236 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
237 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
238 pTmp->size += pArena->next - block;
240 /* Remove the block from the linked list */
242 pTmp->next = pArena->next;
243 pTmp = ARENA_PTR( baseptr, pArena->next );
244 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
248 /***********************************************************************
251 static void LOCAL_PrintHeap( HANDLE16 ds )
254 LOCALHEAPINFO *pInfo;
257 /* FIXME - the test should be done when calling the function!
258 plus is not clear that we should print this info
259 only when TRACE_ON is on! */
260 if(!TRACE_ON(local)) return;
262 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
263 pInfo = LOCAL_GetHeap( ds );
267 DUMP( "Local Heap corrupted! ds=%04x\n", ds );
270 DUMP( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
271 ds, pInfo->first, pInfo->last, pInfo->items );
273 arena = pInfo->first;
276 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
277 DUMP( " %04x: prev=%04x next=%04x type=%d\n", arena,
278 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
279 if (arena == pInfo->first)
281 DUMP( " size=%d free_prev=%04x free_next=%04x\n",
282 pArena->size, pArena->free_prev, pArena->free_next );
284 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
286 DUMP( " size=%d free_prev=%04x free_next=%04x\n",
287 pArena->size, pArena->free_prev, pArena->free_next );
288 if (pArena->next == arena) break; /* last one */
289 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
291 DUMP( "*** arena->free_next->free_prev != arena\n" );
295 if (pArena->next == arena)
297 DUMP( "*** last block is not marked free\n" );
300 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
302 DUMP( "*** arena->next->prev != arena (%04x, %04x)\n",
303 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
306 arena = pArena->next;
311 /***********************************************************************
312 * LocalInit (KERNEL.4)
314 BOOL16 WINAPI LocalInit( HANDLE16 selector, WORD start, WORD end )
317 WORD heapInfoArena, freeArena, lastArena;
318 LOCALHEAPINFO *pHeapInfo;
319 LOCALARENA *pArena, *pFirstArena, *pLastArena;
322 /* The initial layout of the heap is: */
323 /* - first arena (FIXED) */
324 /* - heap info structure (FIXED) */
325 /* - large free block (FREE) */
326 /* - last arena (FREE) */
328 TRACE(local, "%04x %04x-%04x\n", selector, start, end);
329 if (!selector) selector = CURRENT_DS;
333 /* If TRACE_ON(heap) is set, the global heap blocks are */
334 /* cleared before use, so we can test for double initialization. */
335 if (LOCAL_GetHeap(selector))
337 WARN(local, "Heap %04x initialized twice.\n", selector);
338 LOCAL_PrintHeap(selector);
343 /* Check if the segment is the DGROUP of a module */
345 if ((pModule = NE_GetPtr( selector )))
347 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule ) + pModule->dgroup - 1;
348 if (pModule->dgroup && (pSeg->selector == selector)) {
349 /* We can't just use the simple method of using the value
350 * of minsize + stacksize, since there are programs that
351 * resize the data segment before calling InitTask(). So,
352 * we must put it at the end of the segment */
353 start = GlobalSize16( GlobalHandle16( selector ) );
356 TRACE(local," new start %04x, minstart: %04x\n", start, pSeg->minsize + pModule->stack_size);
360 ptr = PTR_SEG_OFF_TO_LIN( selector, 0 );
362 start = LALIGN( MAX( start, sizeof(INSTANCEDATA) ) );
363 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
364 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
365 + sizeof(LOCALHEAPINFO) );
366 lastArena = (end - sizeof(LOCALARENA)) & ~3;
368 /* Make sure there's enough space. */
370 if (freeArena + sizeof(LOCALARENA) >= lastArena) return FALSE;
372 /* Initialise the first arena */
374 pFirstArena = ARENA_PTR( ptr, start );
375 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
376 pFirstArena->next = heapInfoArena;
377 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
378 pFirstArena->free_prev = start; /* this one */
379 pFirstArena->free_next = freeArena;
381 /* Initialise the arena of the heap info structure */
383 pArena = ARENA_PTR( ptr, heapInfoArena );
384 pArena->prev = start | LOCAL_ARENA_FIXED;
385 pArena->next = freeArena;
387 /* Initialise the heap info structure */
389 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
390 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
391 pHeapInfo->items = 4;
392 pHeapInfo->first = start;
393 pHeapInfo->last = lastArena;
394 pHeapInfo->htable = 0;
395 pHeapInfo->hdelta = 0x20;
396 pHeapInfo->extra = 0x200;
397 pHeapInfo->minsize = lastArena - freeArena;
398 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
400 /* Initialise the large free block */
402 pArena = ARENA_PTR( ptr, freeArena );
403 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
404 pArena->next = lastArena;
405 pArena->size = lastArena - freeArena;
406 pArena->free_prev = start;
407 pArena->free_next = lastArena;
409 /* Initialise the last block */
411 pLastArena = ARENA_PTR( ptr, lastArena );
412 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
413 pLastArena->next = lastArena; /* this one */
414 pLastArena->size = LALIGN(sizeof(LOCALARENA));
415 pLastArena->free_prev = freeArena;
416 pLastArena->free_next = lastArena; /* this one */
418 /* Store the local heap address in the instance data */
420 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
421 LOCAL_PrintHeap( selector );
425 /***********************************************************************
428 static void LOCAL_GrowHeap( HANDLE16 ds )
430 HANDLE16 hseg = GlobalHandle16( ds );
431 LONG oldsize = GlobalSize16( hseg );
433 LOCALHEAPINFO *pHeapInfo;
434 WORD freeArena, lastArena;
435 LOCALARENA *pArena, *pLastArena;
438 /* if nothing can be gained, return */
439 if (oldsize > 0xfff0) return;
440 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
441 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
442 pHeapInfo = LOCAL_GetHeap( ds );
443 if (pHeapInfo == NULL) {
444 WARN(local, "Heap not found\n" );
447 end = GlobalSize16( hseg );
448 lastArena = (end - sizeof(LOCALARENA)) & ~3;
450 /* Update the HeapInfo */
452 freeArena = pHeapInfo->last;
453 pHeapInfo->last = lastArena;
454 pHeapInfo->minsize += end - oldsize;
456 /* grow the old last block */
457 pArena = ARENA_PTR( ptr, freeArena );
458 pArena->size = lastArena - freeArena;
459 pArena->next = lastArena;
460 pArena->free_next = lastArena;
462 /* Initialise the new last block */
464 pLastArena = ARENA_PTR( ptr, lastArena );
465 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
466 pLastArena->next = lastArena; /* this one */
467 pLastArena->size = LALIGN(sizeof(LOCALARENA));
468 pLastArena->free_prev = freeArena;
469 pLastArena->free_next = lastArena; /* this one */
471 /* If block before freeArena is also free then merge them */
472 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
474 LOCAL_RemoveBlock(ptr, freeArena);
478 TRACE(local, "Heap expanded\n" );
479 LOCAL_PrintHeap( ds );
483 /***********************************************************************
486 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
488 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
489 LOCALHEAPINFO *pInfo;
490 LOCALARENA *pArena, *pPrev, *pNext;
492 TRACE(local, "%04x ds=%04x\n", arena, ds );
493 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
495 pArena = ARENA_PTR( ptr, arena );
496 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
498 /* shouldn't happen */
499 WARN(local, "Trying to free block %04x twice!\n",
501 LOCAL_PrintHeap( ds );
505 /* Check if we can merge with the previous block */
507 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
508 pNext = ARENA_PTR( ptr, pArena->next );
509 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
511 arena = pArena->prev & ~3;
513 LOCAL_RemoveBlock( ptr, pPrev->next );
516 else /* Make a new free block */
518 LOCAL_MakeBlockFree( ptr, arena );
521 /* Check if we can merge with the next block */
523 if ((pArena->next == pArena->free_next) &&
524 (pArena->next != pInfo->last))
526 LOCAL_RemoveBlock( ptr, pArena->next );
533 /***********************************************************************
536 * Shrink an arena by creating a free block at its end if possible.
537 * 'size' includes the arena header, and must be aligned.
539 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
541 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
542 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
544 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
546 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
548 LOCAL_AddBlock( ptr, arena, arena + size );
550 LOCAL_FreeArena( ds, arena + size );
555 /***********************************************************************
556 * LOCAL_GrowArenaDownward
558 * Grow an arena downward by using the previous arena (must be free).
560 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
562 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
563 LOCALHEAPINFO *pInfo;
564 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
565 WORD prevArena = pArena->prev & ~3;
566 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
570 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
571 offset = pPrevArena->size;
572 size = pArena->next - arena - ARENA_HEADER_SIZE;
573 LOCAL_RemoveFreeBlock( ptr, prevArena );
574 LOCAL_RemoveBlock( ptr, arena );
576 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
577 while (offset < size)
579 memcpy( p, p + offset, offset );
583 if (size) memcpy( p, p + offset, size );
584 LOCAL_ShrinkArena( ds, prevArena, newsize );
589 /***********************************************************************
590 * LOCAL_GrowArenaUpward
592 * Grow an arena upward by using the next arena (must be free and big
593 * enough). Newsize includes the arena header and must be aligned.
595 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
597 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
598 LOCALHEAPINFO *pInfo;
599 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
600 WORD nextArena = pArena->next;
602 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
603 LOCAL_RemoveBlock( ptr, nextArena );
605 LOCAL_ShrinkArena( ds, arena, newsize );
609 /***********************************************************************
612 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
614 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
615 LOCALHEAPINFO *pInfo;
620 if (!(pInfo = LOCAL_GetHeap( ds )))
622 WARN(local, "Local heap not found\n" );
626 arena = pInfo->first;
627 pArena = ARENA_PTR( ptr, arena );
628 while (arena != pArena->free_next)
630 arena = pArena->free_next;
631 pArena = ARENA_PTR( ptr, arena );
632 if (pArena->size >= freespace) freespace = pArena->size;
634 /* FIXME doesn't yet calculate space that would become free if everything
635 were discarded when countdiscard == 1 */
636 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
637 else freespace -= ARENA_HEADER_SIZE;
642 /***********************************************************************
645 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
647 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
648 LOCALHEAPINFO *pInfo;
649 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
650 WORD arena, movearena, finalarena, table;
651 WORD count, movesize, size;
653 LOCALHANDLEENTRY *pEntry;
655 if (!(pInfo = LOCAL_GetHeap( ds )))
657 WARN(local, "Local heap not found\n" );
661 TRACE(local, "ds = %04x, minfree = %04x, flags = %04x\n",
663 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
664 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
666 TRACE(local, "Returning %04x.\n", freespace);
669 TRACE(local, "Compacting heap %04x.\n", ds);
670 table = pInfo->htable;
673 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
674 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
676 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
678 /* OK we can move this one if we want */
679 TRACE(local, "handle %04x (block %04x) can be moved.\n",
680 (WORD)((char *)pEntry - ptr), pEntry->addr);
681 movearena = ARENA_HEADER(pEntry->addr);
682 pMoveArena = ARENA_PTR(ptr, movearena);
683 movesize = pMoveArena->next - movearena;
684 arena = pInfo->first;
685 pArena = ARENA_PTR(ptr, arena);
688 /* Try to find the smallest arena that will do, */
689 /* which is below us in memory */
692 arena = pArena->free_next;
693 pArena = ARENA_PTR(ptr, arena);
694 if(arena >= movearena)
696 if(arena == pArena->free_next)
698 if((pArena->size >= movesize) && (pArena->size < size))
704 if (finalarena) /* Actually got somewhere to move */
706 TRACE(local, "Moving it to %04x.\n", finalarena);
707 pFinalArena = ARENA_PTR(ptr, finalarena);
708 size = pFinalArena->size;
709 LOCAL_RemoveFreeBlock(ptr, finalarena);
710 LOCAL_ShrinkArena( ds, finalarena, movesize );
711 /* Copy the arena to it's new location */
712 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
713 (char *)pMoveArena + ARENA_HEADER_SIZE,
714 movesize - ARENA_HEADER_SIZE );
715 /* Free the old location */
716 LOCAL_FreeArena(ds, movearena);
717 /* Update handle table entry */
718 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
720 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
723 /* Previous arena is free (but < movesize) */
724 /* so we can 'slide' movearena down into it */
725 finalarena = pMoveArena->prev & ~3;
726 LOCAL_GrowArenaDownward( ds, movearena, movesize );
727 /* Update handle table entry */
728 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
732 table = *(WORD *)pEntry;
734 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
735 if(freespace >= minfree || (flags & LMEM_NODISCARD))
737 TRACE(local, "Returning %04x.\n", freespace);
741 table = pInfo->htable;
744 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
745 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
747 if(pEntry->addr && pEntry->lock == 0 &&
748 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
750 TRACE(local, "Discarding handle %04x (block %04x).\n",
751 (char *)pEntry - ptr, pEntry->addr);
752 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
754 pEntry->flags = (LMEM_DISCARDED >> 8);
755 /* Call localnotify proc */
758 table = *(WORD *)pEntry;
760 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
764 /***********************************************************************
765 * LOCAL_FindFreeBlock
767 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
769 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
770 LOCALHEAPINFO *pInfo;
774 if (!(pInfo = LOCAL_GetHeap( ds )))
776 WARN(local, "Local heap not found\n" );
781 arena = pInfo->first;
782 pArena = ARENA_PTR( ptr, arena );
784 arena = pArena->free_next;
785 pArena = ARENA_PTR( ptr, arena );
786 if (arena == pArena->free_next) break;
787 if (pArena->size >= size) return arena;
789 WARN(local, "not enough space\n" );
794 /***********************************************************************
796 * The segment may get moved around in this function, so all callers
797 * should reset their pointer variables.
799 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
801 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
802 LOCALHEAPINFO *pInfo;
806 if (!(pInfo = LOCAL_GetHeap( ds )))
808 WARN(local, "Local heap not found\n");
813 size += ARENA_HEADER_SIZE;
814 size = LALIGN( MAX( size, sizeof(LOCALARENA) ) );
816 /* Find a suitable free block */
817 arena = LOCAL_FindFreeBlock( ds, size );
819 /* no space: try to make some */
820 LOCAL_Compact( ds, size, flags );
821 arena = LOCAL_FindFreeBlock( ds, size );
824 /* still no space: try to grow the segment */
825 LOCAL_GrowHeap( ds );
826 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
827 pInfo = LOCAL_GetHeap( ds );
828 arena = LOCAL_FindFreeBlock( ds, size );
831 if (ds == GDI_HeapSel) {
832 WARN(local, "not enough space in GDI local heap "
833 "(%04x) for %d bytes\n", ds, size );
834 } else if (ds == USER_HeapSel) {
835 WARN(local, "not enough space in USER local heap "
836 "(%04x) for %d bytes\n", ds, size );
838 WARN(local, "not enough space in local heap "
839 "%04x for %d bytes\n", ds, size );
844 /* Make a block out of the free arena */
845 pArena = ARENA_PTR( ptr, arena );
846 TRACE(local, "LOCAL_GetBlock size = %04x, arena %04x size %04x\n",
847 size, arena, pArena->size );
848 LOCAL_RemoveFreeBlock( ptr, arena );
849 LOCAL_ShrinkArena( ds, arena, size );
851 if (flags & LMEM_ZEROINIT)
852 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
853 return arena + ARENA_HEADER_SIZE;
857 /***********************************************************************
860 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
862 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
863 LOCALHEAPINFO *pInfo;
864 LOCALHANDLEENTRY *pEntry;
868 TRACE(local, "Local_NewHTable\n" );
869 if (!(pInfo = LOCAL_GetHeap( ds )))
871 WARN(local, "Local heap not found\n");
876 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
877 + 2 * sizeof(WORD), LMEM_FIXED )))
879 if (!(ptr = PTR_SEG_OFF_TO_LIN( ds, 0 )))
880 WARN(local, "ptr == NULL after GetBlock.\n");
881 if (!(pInfo = LOCAL_GetHeap( ds )))
882 WARN(local,"pInfo == NULL after GetBlock.\n");
884 /* Fill the entry table */
886 *(WORD *)(ptr + handle) = pInfo->hdelta;
887 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
888 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
889 pEntry->lock = pEntry->flags = 0xff;
892 *(WORD *)pEntry = pInfo->htable;
893 pInfo->htable = handle;
898 /***********************************************************************
899 * LOCAL_GetNewHandleEntry
901 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
903 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
904 LOCALHEAPINFO *pInfo;
905 LOCALHANDLEENTRY *pEntry = NULL;
908 if (!(pInfo = LOCAL_GetHeap( ds )))
910 WARN(local, "Local heap not found\n");
915 /* Find a free slot in existing tables */
917 table = pInfo->htable;
920 WORD count = *(WORD *)(ptr + table);
921 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
922 for (; count > 0; count--, pEntry++)
923 if (pEntry->lock == 0xff) break;
925 table = *(WORD *)pEntry;
928 if (!table) /* We need to create a new table */
930 if (!LOCAL_NewHTable( ds )) return 0;
931 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
932 pInfo = LOCAL_GetHeap( ds );
933 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
936 /* Now allocate this entry */
940 TRACE(local, "(%04x): %04x\n",
941 ds, ((char *)pEntry - ptr) );
942 return (HLOCAL16)((char *)pEntry - ptr);
946 /***********************************************************************
947 * LOCAL_FreeHandleEntry
949 * Free a handle table entry.
951 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
953 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
954 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
955 LOCALHEAPINFO *pInfo;
957 WORD table, count, i;
959 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
961 /* Find the table where this handle comes from */
963 pTable = &pInfo->htable;
966 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
967 if ((handle >= *pTable + sizeof(WORD)) &&
968 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
969 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
973 WARN(local, "Invalid entry %04x\n", handle);
974 LOCAL_PrintHeap( ds );
978 /* Make the entry free */
980 pEntry->addr = 0; /* just in case */
982 pEntry->flags = 0xff;
983 /* Now check if all entries in this table are free */
986 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
987 count = *(WORD *)(ptr + table);
988 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
990 /* Remove the table from the linked list and free it */
992 TRACE(local, "(%04x): freeing table %04x\n",
994 *pTable = *(WORD *)pEntry;
995 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
999 /***********************************************************************
1002 * Implementation of LocalFree().
1004 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1006 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1008 TRACE(local, "%04x ds=%04x\n", handle, ds );
1010 if (!handle) { WARN(local, "Handle is 0.\n" ); return 0; }
1011 if (HANDLE_FIXED( handle ))
1013 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1014 else return handle; /* couldn't free it */
1018 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1019 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1021 TRACE(local, "real block at %04x\n",
1023 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr) ))
1024 return handle; /* couldn't free it */
1026 LOCAL_FreeHandleEntry( ds, handle );
1032 /***********************************************************************
1035 * Implementation of LocalAlloc().
1038 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1043 TRACE(local, "%04x %d ds=%04x\n", flags, size, ds );
1045 if(size > 0 && size <= 4) size = 5;
1046 if (flags & LMEM_MOVEABLE)
1048 LOCALHANDLEENTRY *plhe;
1053 if (!(hmem = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags )))
1056 else /* We just need to allocate a discarded handle */
1058 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1060 WARN(local, "Couldn't get handle.\n");
1062 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1065 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1066 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1070 plhe->addr = hmem + sizeof(HLOCAL16);
1071 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1072 *(HLOCAL16 *)(ptr + hmem) = handle;
1077 plhe->flags = LMEM_DISCARDED >> 8;
1084 handle = LOCAL_GetBlock( ds, size, flags );
1090 /***********************************************************************
1093 * Implementation of LocalReAlloc().
1095 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1097 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1098 LOCALHEAPINFO *pInfo;
1099 LOCALARENA *pArena, *pNext;
1100 LOCALHANDLEENTRY *pEntry = NULL;
1101 WORD arena, oldsize;
1102 HLOCAL16 hmem, blockhandle;
1105 if (!handle) return 0;
1106 if(HANDLE_MOVEABLE(handle) &&
1107 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1110 TRACE(local, "%04x %d %04x ds=%04x\n",
1111 handle, size, flags, ds );
1112 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1114 if (HANDLE_FIXED( handle ))
1115 blockhandle = handle;
1118 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1119 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1123 WARN(local,"Dicarded block has non-zero addr.\n");
1124 TRACE(local, "ReAllocating discarded block\n");
1125 if(size <= 4) size = 5;
1126 if (!(hl = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags)))
1128 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1129 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1130 pEntry->addr = hl + sizeof(HLOCAL16);
1133 *(HLOCAL16 *)(ptr + hl) = handle;
1136 if (((blockhandle = pEntry->addr) & 3) != 2)
1138 WARN(local, "(%04x,%04x): invalid handle\n",
1142 if(*((HLOCAL16 *)(ptr + blockhandle) - 1) != handle) {
1143 WARN(local, "Back ptr to handle is invalid\n");
1148 if (flags & LMEM_MODIFY)
1150 if (HANDLE_MOVEABLE(handle))
1152 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1153 pEntry->flags = (flags & 0x0f00) >> 8;
1154 TRACE(local, "Changing flags to %x.\n", pEntry->flags);
1161 if (flags & LMEM_MOVEABLE)
1163 if (HANDLE_FIXED(handle))
1165 TRACE(local, "Freeing fixed block.\n");
1166 return LOCAL_Free( ds, handle );
1168 else /* Moveable block */
1170 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1171 if (pEntry->lock == 0)
1173 /* discards moveable blocks */
1174 TRACE(local,"Discarding block\n");
1175 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
1177 pEntry->flags = (LMEM_DISCARDED >> 8);
1185 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1186 if (pEntry->lock == 0)
1189 return LOCAL_Free( ds, handle );
1195 arena = ARENA_HEADER( blockhandle );
1196 TRACE(local, "arena is %04x\n", arena );
1197 pArena = ARENA_PTR( ptr, arena );
1199 if(size <= 4) size = 5;
1200 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1201 nextarena = LALIGN(blockhandle + size);
1203 /* Check for size reduction */
1205 if (nextarena <= pArena->next)
1207 TRACE(local, "size reduction, making new free block\n");
1208 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1209 TRACE(local, "returning %04x\n", handle );
1213 /* Check if the next block is free and large enough */
1215 pNext = ARENA_PTR( ptr, pArena->next );
1216 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1217 (nextarena <= pNext->next))
1219 TRACE(local, "size increase, making new free block\n");
1220 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1221 TRACE(local, "returning %04x\n", handle );
1225 /* Now we have to allocate a new block, but not if (fixed block or locked
1226 block) and no LMEM_MOVEABLE */
1228 if (!(flags & LMEM_MOVEABLE))
1230 if (HANDLE_FIXED(handle))
1232 WARN(local, "Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1237 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1239 WARN(local, "Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1244 if(HANDLE_MOVEABLE(handle)) size += sizeof(HLOCAL16);
1245 hmem = LOCAL_GetBlock( ds, size, flags );
1246 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1247 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1248 { /* a compaction, which might in turn have */
1249 blockhandle = pEntry->addr ; /* moved the very block we are resizing */
1250 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1254 /* Remove the block from the heap and try again */
1255 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1256 if (!buffer) return 0;
1257 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1258 LOCAL_FreeArena( ds, arena );
1259 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1261 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1263 WARN(local, "Can't restore saved block\n" );
1264 HeapFree( GetProcessHeap(), 0, buffer );
1269 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1270 memcpy( ptr + hmem, buffer, oldsize );
1271 HeapFree( GetProcessHeap(), 0, buffer );
1275 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1276 LOCAL_FreeArena( ds, arena );
1278 if (HANDLE_MOVEABLE( handle ))
1280 TRACE(local, "fixing handle\n");
1281 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1282 pEntry->addr = hmem + sizeof(HLOCAL16);
1283 /* Back ptr should still be correct */
1284 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1285 WARN(local, "back ptr is invalid.\n");
1288 if (size == oldsize) hmem = 0; /* Realloc failed */
1289 TRACE(local, "returning %04x\n", hmem );
1294 /***********************************************************************
1295 * LOCAL_InternalLock
1297 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1299 HLOCAL16 old_handle = handle;
1301 if (HANDLE_MOVEABLE(handle))
1303 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1304 if (pEntry->flags == LMEM_DISCARDED) return 0;
1305 if (pEntry->lock < 0xfe) pEntry->lock++;
1306 handle = pEntry->addr;
1308 TRACE(local, "%04x returning %04x\n",
1309 old_handle, handle );
1314 /***********************************************************************
1317 LPSTR LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1319 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1320 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1324 /***********************************************************************
1327 SEGPTR LOCAL_LockSegptr( HANDLE16 ds, HLOCAL16 handle )
1329 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1330 return PTR_SEG_OFF_TO_SEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1334 /***********************************************************************
1337 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1339 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1341 TRACE(local, "%04x\n", handle );
1342 if (HANDLE_MOVEABLE(handle))
1344 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1345 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1346 /* For moveable block, return the new lock count */
1347 /* (see _Windows_Internals_ p. 197) */
1348 return --pEntry->lock;
1354 /***********************************************************************
1357 * Implementation of LocalSize().
1359 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1361 char *ptr = PTR_SEG_OFF_TO_LIN( CURRENT_DS, 0 );
1364 TRACE(local, "%04x ds=%04x\n", handle, ds );
1366 if (HANDLE_MOVEABLE( handle )) handle = *(WORD *)(ptr + handle);
1367 if (!handle) return 0;
1368 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1369 return pArena->next - handle;
1373 /***********************************************************************
1376 * Implementation of LocalFlags().
1378 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1380 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1382 if (HANDLE_MOVEABLE(handle))
1384 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1385 TRACE(local, "(%04x,%04x): returning %04x\n",
1386 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1387 return pEntry->lock | (pEntry->flags << 8);
1391 TRACE(local, "(%04x,%04x): returning 0\n",
1398 /***********************************************************************
1401 * Implementation of LocalHeapSize().
1403 WORD LOCAL_HeapSize( HANDLE16 ds )
1405 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1406 if (!pInfo) return 0;
1407 return pInfo->last - pInfo->first;
1411 /***********************************************************************
1414 * Implementation of LocalCountFree().
1416 WORD LOCAL_CountFree( HANDLE16 ds )
1420 LOCALHEAPINFO *pInfo;
1421 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1423 if (!(pInfo = LOCAL_GetHeap( ds )))
1425 WARN(local, "(%04x): Local heap not found\n", ds );
1426 LOCAL_PrintHeap( ds );
1431 arena = pInfo->first;
1432 pArena = ARENA_PTR( ptr, arena );
1435 arena = pArena->free_next;
1436 pArena = ARENA_PTR( ptr, arena );
1437 if (arena == pArena->free_next) break;
1438 total += pArena->size;
1440 TRACE(local, "(%04x): returning %d\n", ds, total);
1445 /***********************************************************************
1448 * Implementation of LocalHandle().
1450 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1452 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1453 LOCALHEAPINFO *pInfo;
1456 if (!(pInfo = LOCAL_GetHeap( ds )))
1458 WARN(local, "(%04x): Local heap not found\n", ds );
1459 LOCAL_PrintHeap( ds );
1463 /* Find the address in the entry tables */
1465 table = pInfo->htable;
1468 WORD count = *(WORD *)(ptr + table);
1469 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1470 for (; count > 0; count--, pEntry++)
1471 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1472 table = *(WORD *)pEntry;
1475 return (HLOCAL16)addr; /* Fixed block handle is addr */
1479 /***********************************************************************
1480 * LocalAlloc16 (KERNEL.5)
1482 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1484 return LOCAL_Alloc( CURRENT_DS, flags, size );
1488 /***********************************************************************
1489 * LocalReAlloc16 (KERNEL.6)
1491 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1493 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1497 /***********************************************************************
1498 * LocalFree16 (KERNEL.7)
1500 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1502 return LOCAL_Free( CURRENT_DS, handle );
1506 /***********************************************************************
1507 * LocalLock16 (KERNEL.8)
1509 * Note: only the offset part of the pointer is returned by the relay code.
1511 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1513 return LOCAL_LockSegptr( CURRENT_DS, handle );
1517 /***********************************************************************
1518 * LocalUnlock16 (KERNEL.9)
1520 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1522 return LOCAL_Unlock( CURRENT_DS, handle );
1526 /***********************************************************************
1527 * LocalSize16 (KERNEL.10)
1529 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1531 return LOCAL_Size( CURRENT_DS, handle );
1535 /***********************************************************************
1536 * LocalHandle16 (KERNEL.11)
1538 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1540 return LOCAL_Handle( CURRENT_DS, addr );
1544 /***********************************************************************
1545 * LocalFlags16 (KERNEL.12)
1547 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1549 return LOCAL_Flags( CURRENT_DS, handle );
1553 /***********************************************************************
1554 * LocalCompact16 (KERNEL.13)
1556 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1558 TRACE(local, "%04x\n", minfree );
1559 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1563 /***********************************************************************
1564 * LocalNotify (KERNEL.14)
1566 FARPROC16 WINAPI LocalNotify( FARPROC16 func )
1568 LOCALHEAPINFO *pInfo;
1569 FARPROC16 oldNotify;
1570 HANDLE16 ds = CURRENT_DS;
1572 if (!(pInfo = LOCAL_GetHeap( ds )))
1574 ERR(local, "(%04x): Local heap not found\n", ds );
1575 LOCAL_PrintHeap( ds );
1578 TRACE(local, "(%04x): %08lx\n", ds, (DWORD)func );
1579 FIXME(local, "Half implemented\n");
1580 oldNotify = pInfo->notify;
1581 pInfo->notify = func;
1586 /***********************************************************************
1587 * LocalShrink16 (KERNEL.121)
1589 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1591 TRACE(local, "%04x %04x\n", handle, newsize );
1596 /***********************************************************************
1597 * GetHeapSpaces (KERNEL.138)
1599 DWORD WINAPI GetHeapSpaces( HMODULE16 module )
1604 if (!(pModule = NE_GetPtr( module ))) return 0;
1605 ds = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
1606 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1610 /***********************************************************************
1611 * LocalCountFree (KERNEL.161)
1613 WORD WINAPI LocalCountFree(void)
1615 return LOCAL_CountFree( CURRENT_DS );
1619 /***********************************************************************
1620 * LocalHeapSize (KERNEL.162)
1622 WORD WINAPI LocalHeapSize(void)
1624 TRACE(local, "(void)\n" );
1625 return LOCAL_HeapSize( CURRENT_DS );
1629 /***********************************************************************
1630 * LocalHandleDelta (KERNEL.310)
1632 WORD WINAPI LocalHandleDelta( WORD delta )
1634 LOCALHEAPINFO *pInfo;
1636 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1638 ERR(local, "Local heap not found\n");
1639 LOCAL_PrintHeap( CURRENT_DS );
1642 if (delta) pInfo->hdelta = delta;
1643 TRACE(local, "returning %04x\n", pInfo->hdelta);
1644 return pInfo->hdelta;
1648 /***********************************************************************
1649 * LocalInfo (TOOLHELP.56)
1651 BOOL16 WINAPI LocalInfo( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1653 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(WIN16_GlobalLock16(handle)));
1654 if (!pInfo) return FALSE;
1655 pLocalInfo->wcItems = pInfo->items;
1660 /***********************************************************************
1661 * LocalFirst (TOOLHELP.57)
1663 BOOL16 WINAPI LocalFirst( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1665 WORD ds = GlobalHandleToSel( handle );
1666 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1667 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1668 if (!pInfo) return FALSE;
1670 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1671 pLocalEntry->wAddress = pLocalEntry->hHandle;
1672 pLocalEntry->wFlags = LF_FIXED;
1673 pLocalEntry->wcLock = 0;
1674 pLocalEntry->wType = LT_NORMAL;
1675 pLocalEntry->hHeap = handle;
1676 pLocalEntry->wHeapType = NORMAL_HEAP;
1677 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1678 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1683 /***********************************************************************
1684 * LocalNext (TOOLHELP.58)
1686 BOOL16 WINAPI LocalNext( LOCALENTRY *pLocalEntry )
1688 WORD ds = GlobalHandleToSel( pLocalEntry->hHeap );
1689 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1692 if (!LOCAL_GetHeap( ds )) return FALSE;
1693 if (!pLocalEntry->wNext) return FALSE;
1694 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1696 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1697 pLocalEntry->wAddress = pLocalEntry->hHandle;
1698 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1699 pLocalEntry->wcLock = 0;
1700 pLocalEntry->wType = LT_NORMAL;
1701 if (pArena->next != pLocalEntry->wNext) /* last one? */
1702 pLocalEntry->wNext = pArena->next;
1704 pLocalEntry->wNext = 0;
1705 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1710 /***********************************************************************
1711 * LocalAlloc32 (KERNEL32.371)
1716 HLOCAL32 WINAPI LocalAlloc32(
1717 UINT32 flags, /* [in] Allocation attributes */
1718 DWORD size /* [in] Number of bytes to allocate */
1720 return (HLOCAL32)GlobalAlloc32( flags, size );
1724 /***********************************************************************
1725 * LocalCompact32 (KERNEL32.372)
1727 UINT32 WINAPI LocalCompact32( UINT32 minfree )
1729 return 0; /* LocalCompact does nothing in Win32 */
1733 /***********************************************************************
1734 * LocalFlags32 (KERNEL32.374)
1736 * Value specifying allocation flags and lock count.
1737 * LMEM_INVALID_HANDLE: Failure
1739 UINT32 WINAPI LocalFlags32(
1740 HLOCAL32 handle /* [in] Handle of memory object */
1742 return GlobalFlags32( (HGLOBAL32)handle );
1746 /***********************************************************************
1747 * LocalFree32 (KERNEL32.375)
1752 HLOCAL32 WINAPI LocalFree32(
1753 HLOCAL32 handle /* [in] Handle of memory object */
1755 return (HLOCAL32)GlobalFree32( (HGLOBAL32)handle );
1759 /***********************************************************************
1760 * LocalHandle32 (KERNEL32.376)
1765 HLOCAL32 WINAPI LocalHandle32(
1766 LPCVOID ptr /* [in] Address of local memory object */
1768 return (HLOCAL32)GlobalHandle32( ptr );
1772 /***********************************************************************
1773 * LocalLock32 (KERNEL32.377)
1774 * Locks a local memory object and returns pointer to the first byte
1775 * of the memory block.
1781 LPVOID WINAPI LocalLock32(
1782 HLOCAL32 handle /* [in] Address of local memory object */
1784 return GlobalLock32( (HGLOBAL32)handle );
1788 /***********************************************************************
1789 * LocalReAlloc32 (KERNEL32.378)
1794 HLOCAL32 WINAPI LocalReAlloc32(
1795 HLOCAL32 handle, /* [in] Handle of memory object */
1796 DWORD size, /* [in] New size of block */
1797 UINT32 flags /* [in] How to reallocate object */
1799 return (HLOCAL32)GlobalReAlloc32( (HGLOBAL32)handle, size, flags );
1803 /***********************************************************************
1804 * LocalShrink32 (KERNEL32.379)
1806 UINT32 WINAPI LocalShrink32( HGLOBAL32 handle, UINT32 newsize )
1808 return 0; /* LocalShrink does nothing in Win32 */
1812 /***********************************************************************
1813 * LocalSize32 (KERNEL32.380)
1818 UINT32 WINAPI LocalSize32(
1819 HLOCAL32 handle /* [in] Handle of memory object */
1821 return GlobalSize32( (HGLOBAL32)handle );
1825 /***********************************************************************
1826 * LocalUnlock32 (KERNEL32.381)
1828 * TRUE: Object is still locked
1829 * FALSE: Object is unlocked
1831 BOOL32 WINAPI LocalUnlock32(
1832 HLOCAL32 handle /* [in] Handle of memory object */
1834 return GlobalUnlock32( (HGLOBAL32)handle );