4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Huw Davies
10 * All local heap functions need the current DS as first parameter
11 * when called from the emulation library, so they take one more
12 * parameter than usual.
17 #include "wine/winbase16.h"
25 #include "stackframe.h"
27 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(local)
31 DECLARE_DEBUG_CHANNEL(heap)
36 WORD prev; /* Previous arena | arena type */
37 WORD next; /* Next arena */
38 /* Start of the memory block or free-list info */
39 WORD size; /* Size of the free block */
40 WORD free_prev; /* Previous free block */
41 WORD free_next; /* Next free block */
44 #define ARENA_HEADER_SIZE 4
45 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
47 /* Arena types (stored in 'prev' field of the arena) */
48 #define LOCAL_ARENA_FREE 0
49 #define LOCAL_ARENA_FIXED 1
51 /* LocalNotify() msgs */
57 /* Layout of a handle entry table
59 * WORD count of entries
60 * LOCALHANDLEENTRY[count] entries
61 * WORD near ptr to next table
65 WORD addr; /* Address of the MOVEABLE block */
66 BYTE flags; /* Flags for this block */
67 BYTE lock; /* Lock count */
71 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
72 * in case something actually relies on this.
73 * Note the ARENA_HEADER(addr) still produces the desired result ie. 4n - 4
75 * An unused handle has lock = flags = 0xff. In windows addr is that of next
76 * free handle, at the moment in wine we set it to 0.
78 * A discarded block's handle has lock = addr = 0 and flags = 0x40
79 * (LMEM_DISCARDED >> 8)
86 WORD check; /* 00 Heap checking flag */
87 WORD freeze; /* 02 Heap frozen flag */
88 WORD items; /* 04 Count of items on the heap */
89 WORD first; /* 06 First item of the heap */
90 WORD pad1; /* 08 Always 0 */
91 WORD last; /* 0a Last item of the heap */
92 WORD pad2; /* 0c Always 0 */
93 BYTE ncompact; /* 0e Compactions counter */
94 BYTE dislevel; /* 0f Discard level */
95 DWORD distotal; /* 10 Total bytes discarded */
96 WORD htable; /* 14 Pointer to handle table */
97 WORD hfree; /* 16 Pointer to free handle table */
98 WORD hdelta; /* 18 Delta to expand the handle table */
99 WORD expand; /* 1a Pointer to expand function (unused) */
100 WORD pstat; /* 1c Pointer to status structure (unused) */
101 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
102 WORD lock; /* 22 Lock count for the heap */
103 WORD extra; /* 24 Extra bytes to allocate when expanding */
104 WORD minsize; /* 26 Minimum size of the heap */
105 WORD magic; /* 28 Magic number */
110 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
112 WORD USER_HeapSel = 0; /* USER heap selector */
113 WORD GDI_HeapSel = 0; /* GDI heap selector */
115 /* All local heap allocations are aligned on 4-byte boundaries */
116 #define LALIGN(word) (((word) + 3) & ~3)
118 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
119 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
120 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
121 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
123 /* determine whether the handle belongs to a fixed or a moveable block */
124 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
125 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
127 /***********************************************************************
130 * Return a pointer to the local heap, making sure it exists.
132 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
134 LOCALHEAPINFO *pInfo;
135 INSTANCEDATA *ptr = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( ds, 0 );
136 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
137 if (!ptr || !ptr->heap) return NULL;
138 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
140 WARN("Bad pointer\n");
143 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
144 if (pInfo->magic != LOCAL_HEAP_MAGIC)
153 /***********************************************************************
154 * LOCAL_MakeBlockFree
156 * Make a block free, inserting it in the free-list.
157 * 'block' is the handle of the block arena; 'baseptr' points to
158 * the beginning of the data segment containing the heap.
160 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
162 LOCALARENA *pArena, *pNext;
165 /* Mark the block as free */
167 pArena = ARENA_PTR( baseptr, block );
168 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
169 pArena->size = pArena->next - block;
171 /* Find the next free block (last block is always free) */
176 pNext = ARENA_PTR( baseptr, next );
177 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
181 TRACE("%04x, next %04x\n", block, next );
182 /* Insert the free block in the free-list */
184 pArena->free_prev = pNext->free_prev;
185 pArena->free_next = next;
186 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
187 pNext->free_prev = block;
191 /***********************************************************************
192 * LOCAL_RemoveFreeBlock
194 * Remove a block from the free-list.
195 * 'block' is the handle of the block arena; 'baseptr' points to
196 * the beginning of the data segment containing the heap.
198 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
200 /* Mark the block as fixed */
202 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
203 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
205 /* Remove it from the list */
207 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
208 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
212 /***********************************************************************
215 * Insert a new block in the heap.
216 * 'new' is the handle of the new block arena; 'baseptr' points to
217 * the beginning of the data segment containing the heap; 'prev' is
218 * the block before the new one.
220 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
222 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
223 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
225 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
226 pNew->next = pPrev->next;
227 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
228 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
233 /***********************************************************************
236 * Remove a block from the heap.
237 * 'block' is the handle of the block arena; 'baseptr' points to
238 * the beginning of the data segment containing the heap.
240 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
242 LOCALARENA *pArena, *pTmp;
244 /* Remove the block from the free-list */
247 pArena = ARENA_PTR( baseptr, block );
248 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
249 LOCAL_RemoveFreeBlock( baseptr, block );
251 /* If the previous block is free, expand its size */
253 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
254 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
255 pTmp->size += pArena->next - block;
257 /* Remove the block from the linked list */
259 pTmp->next = pArena->next;
260 pTmp = ARENA_PTR( baseptr, pArena->next );
261 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
265 /***********************************************************************
268 static void LOCAL_PrintHeap( HANDLE16 ds )
271 LOCALHEAPINFO *pInfo;
274 /* FIXME - the test should be done when calling the function!
275 plus is not clear that we should print this info
276 only when TRACE_ON is on! */
277 if(!TRACE_ON(local)) return;
279 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
280 pInfo = LOCAL_GetHeap( ds );
284 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
287 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
288 ds, pInfo->first, pInfo->last, pInfo->items );
290 arena = pInfo->first;
293 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
294 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
295 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
296 if (arena == pInfo->first)
298 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
299 pArena->size, pArena->free_prev, pArena->free_next );
301 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
303 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
304 pArena->size, pArena->free_prev, pArena->free_next );
305 if (pArena->next == arena) break; /* last one */
306 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
308 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
312 if (pArena->next == arena)
314 DPRINTF( "*** last block is not marked free\n" );
317 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
319 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
320 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
323 arena = pArena->next;
328 /***********************************************************************
329 * LocalInit (KERNEL.4)
331 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
334 WORD heapInfoArena, freeArena, lastArena;
335 LOCALHEAPINFO *pHeapInfo;
336 LOCALARENA *pArena, *pFirstArena, *pLastArena;
340 /* The initial layout of the heap is: */
341 /* - first arena (FIXED) */
342 /* - heap info structure (FIXED) */
343 /* - large free block (FREE) */
344 /* - last arena (FREE) */
346 TRACE("%04x %04x-%04x\n", selector, start, end);
347 if (!selector) selector = CURRENT_DS;
351 /* If TRACE_ON(heap) is set, the global heap blocks are */
352 /* cleared before use, so we can test for double initialization. */
353 if (LOCAL_GetHeap(selector))
355 ERR("Heap %04x initialized twice.\n", selector);
356 LOCAL_PrintHeap(selector);
362 /* start == 0 means: put the local heap at the end of the segment */
364 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
365 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
366 if ( end > 0xfffe ) end = 0xfffe;
372 if ((pModule = NE_GetPtr( GlobalHandle16( selector ) )))
374 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
377 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
378 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
381 if ( segNr < pModule->seg_count )
383 WORD minsize = pSeg->minsize;
384 if ( pModule->ss == segNr+1 )
385 minsize += pModule->stack_size;
387 TRACE(" new start %04x, minstart: %04x\n", start, minsize);
391 ptr = PTR_SEG_OFF_TO_LIN( selector, 0 );
393 start = LALIGN( MAX( start, sizeof(INSTANCEDATA) ) );
394 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
395 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
396 + sizeof(LOCALHEAPINFO) );
397 lastArena = (end - sizeof(LOCALARENA)) & ~3;
399 /* Make sure there's enough space. */
401 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
403 /* Initialise the first arena */
405 pFirstArena = ARENA_PTR( ptr, start );
406 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
407 pFirstArena->next = heapInfoArena;
408 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
409 pFirstArena->free_prev = start; /* this one */
410 pFirstArena->free_next = freeArena;
412 /* Initialise the arena of the heap info structure */
414 pArena = ARENA_PTR( ptr, heapInfoArena );
415 pArena->prev = start | LOCAL_ARENA_FIXED;
416 pArena->next = freeArena;
418 /* Initialise the heap info structure */
420 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
421 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
422 pHeapInfo->items = 4;
423 pHeapInfo->first = start;
424 pHeapInfo->last = lastArena;
425 pHeapInfo->htable = 0;
426 pHeapInfo->hdelta = 0x20;
427 pHeapInfo->extra = 0x200;
428 pHeapInfo->minsize = lastArena - freeArena;
429 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
431 /* Initialise the large free block */
433 pArena = ARENA_PTR( ptr, freeArena );
434 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
435 pArena->next = lastArena;
436 pArena->size = lastArena - freeArena;
437 pArena->free_prev = start;
438 pArena->free_next = lastArena;
440 /* Initialise the last block */
442 pLastArena = ARENA_PTR( ptr, lastArena );
443 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
444 pLastArena->next = lastArena; /* this one */
445 pLastArena->size = LALIGN(sizeof(LOCALARENA));
446 pLastArena->free_prev = freeArena;
447 pLastArena->free_next = lastArena; /* this one */
449 /* Store the local heap address in the instance data */
451 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
452 LOCAL_PrintHeap( selector );
456 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
460 /***********************************************************************
463 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
468 LOCALHEAPINFO *pHeapInfo;
469 WORD freeArena, lastArena;
470 LOCALARENA *pArena, *pLastArena;
473 hseg = GlobalHandle16( ds );
474 /* maybe mem allocated by Virtual*() ? */
475 if (!hseg) return FALSE;
477 oldsize = GlobalSize16( hseg );
478 /* if nothing can be gained, return */
479 if (oldsize > 0xfff0) return FALSE;
480 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
481 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
482 pHeapInfo = LOCAL_GetHeap( ds );
483 if (pHeapInfo == NULL) {
484 ERR("Heap not found\n" );
487 end = GlobalSize16( hseg );
488 lastArena = (end - sizeof(LOCALARENA)) & ~3;
490 /* Update the HeapInfo */
492 freeArena = pHeapInfo->last;
493 pHeapInfo->last = lastArena;
494 pHeapInfo->minsize += end - oldsize;
496 /* grow the old last block */
497 pArena = ARENA_PTR( ptr, freeArena );
498 pArena->size = lastArena - freeArena;
499 pArena->next = lastArena;
500 pArena->free_next = lastArena;
502 /* Initialise the new last block */
504 pLastArena = ARENA_PTR( ptr, lastArena );
505 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
506 pLastArena->next = lastArena; /* this one */
507 pLastArena->size = LALIGN(sizeof(LOCALARENA));
508 pLastArena->free_prev = freeArena;
509 pLastArena->free_next = lastArena; /* this one */
511 /* If block before freeArena is also free then merge them */
512 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
514 LOCAL_RemoveBlock(ptr, freeArena);
518 TRACE("Heap expanded\n" );
519 LOCAL_PrintHeap( ds );
524 /***********************************************************************
527 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
529 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
530 LOCALHEAPINFO *pInfo;
531 LOCALARENA *pArena, *pPrev, *pNext;
533 TRACE("%04x ds=%04x\n", arena, ds );
534 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
536 pArena = ARENA_PTR( ptr, arena );
537 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
539 /* shouldn't happen */
540 ERR("Trying to free block %04x twice!\n",
542 LOCAL_PrintHeap( ds );
546 /* Check if we can merge with the previous block */
548 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
549 pNext = ARENA_PTR( ptr, pArena->next );
550 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
552 arena = pArena->prev & ~3;
554 LOCAL_RemoveBlock( ptr, pPrev->next );
557 else /* Make a new free block */
559 LOCAL_MakeBlockFree( ptr, arena );
562 /* Check if we can merge with the next block */
564 if ((pArena->next == pArena->free_next) &&
565 (pArena->next != pInfo->last))
567 LOCAL_RemoveBlock( ptr, pArena->next );
574 /***********************************************************************
577 * Shrink an arena by creating a free block at its end if possible.
578 * 'size' includes the arena header, and must be aligned.
580 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
582 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
583 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
585 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
587 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
589 LOCAL_AddBlock( ptr, arena, arena + size );
591 LOCAL_FreeArena( ds, arena + size );
596 /***********************************************************************
597 * LOCAL_GrowArenaDownward
599 * Grow an arena downward by using the previous arena (must be free).
601 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
603 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
604 LOCALHEAPINFO *pInfo;
605 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
606 WORD prevArena = pArena->prev & ~3;
607 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
611 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
612 offset = pPrevArena->size;
613 size = pArena->next - arena - ARENA_HEADER_SIZE;
614 LOCAL_RemoveFreeBlock( ptr, prevArena );
615 LOCAL_RemoveBlock( ptr, arena );
617 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
618 while (offset < size)
620 memcpy( p, p + offset, offset );
624 if (size) memcpy( p, p + offset, size );
625 LOCAL_ShrinkArena( ds, prevArena, newsize );
630 /***********************************************************************
631 * LOCAL_GrowArenaUpward
633 * Grow an arena upward by using the next arena (must be free and big
634 * enough). Newsize includes the arena header and must be aligned.
636 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
638 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
639 LOCALHEAPINFO *pInfo;
640 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
641 WORD nextArena = pArena->next;
643 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
644 LOCAL_RemoveBlock( ptr, nextArena );
646 LOCAL_ShrinkArena( ds, arena, newsize );
650 /***********************************************************************
653 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
655 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
656 LOCALHEAPINFO *pInfo;
661 if (!(pInfo = LOCAL_GetHeap( ds )))
663 ERR("Local heap not found\n" );
667 arena = pInfo->first;
668 pArena = ARENA_PTR( ptr, arena );
669 while (arena != pArena->free_next)
671 arena = pArena->free_next;
672 pArena = ARENA_PTR( ptr, arena );
673 if (pArena->size >= freespace) freespace = pArena->size;
675 /* FIXME doesn't yet calculate space that would become free if everything
676 were discarded when countdiscard == 1 */
677 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
678 else freespace -= ARENA_HEADER_SIZE;
683 /***********************************************************************
686 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
688 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
689 LOCALHEAPINFO *pInfo;
690 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
691 WORD arena, movearena, finalarena, table;
692 WORD count, movesize, size;
694 LOCALHANDLEENTRY *pEntry;
696 if (!(pInfo = LOCAL_GetHeap( ds )))
698 ERR("Local heap not found\n" );
702 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
704 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
705 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
707 TRACE("Returning %04x.\n", freespace);
710 TRACE("Compacting heap %04x.\n", ds);
711 table = pInfo->htable;
714 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
715 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
717 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
719 /* OK we can move this one if we want */
720 TRACE("handle %04x (block %04x) can be moved.\n",
721 (WORD)((char *)pEntry - ptr), pEntry->addr);
722 movearena = ARENA_HEADER(pEntry->addr);
723 pMoveArena = ARENA_PTR(ptr, movearena);
724 movesize = pMoveArena->next - movearena;
725 arena = pInfo->first;
726 pArena = ARENA_PTR(ptr, arena);
729 /* Try to find the smallest arena that will do, */
730 /* which is below us in memory */
733 arena = pArena->free_next;
734 pArena = ARENA_PTR(ptr, arena);
735 if(arena >= movearena)
737 if(arena == pArena->free_next)
739 if((pArena->size >= movesize) && (pArena->size < size))
745 if (finalarena) /* Actually got somewhere to move */
747 TRACE("Moving it to %04x.\n", finalarena);
748 pFinalArena = ARENA_PTR(ptr, finalarena);
749 size = pFinalArena->size;
750 LOCAL_RemoveFreeBlock(ptr, finalarena);
751 LOCAL_ShrinkArena( ds, finalarena, movesize );
752 /* Copy the arena to it's new location */
753 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
754 (char *)pMoveArena + ARENA_HEADER_SIZE,
755 movesize - ARENA_HEADER_SIZE );
756 /* Free the old location */
757 LOCAL_FreeArena(ds, movearena);
759 Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_MOVE,
760 (WORD)((char *)pEntry - ptr), pEntry->addr);
761 /* Update handle table entry */
762 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
764 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
767 /* Previous arena is free (but < movesize) */
768 /* so we can 'slide' movearena down into it */
769 finalarena = pMoveArena->prev & ~3;
770 LOCAL_GrowArenaDownward( ds, movearena, movesize );
771 /* Update handle table entry */
772 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
776 table = *(WORD *)pEntry;
778 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
779 if(freespace >= minfree || (flags & LMEM_NODISCARD))
781 TRACE("Returning %04x.\n", freespace);
785 table = pInfo->htable;
788 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
789 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
791 if(pEntry->addr && pEntry->lock == 0 &&
792 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
794 TRACE("Discarding handle %04x (block %04x).\n",
795 (char *)pEntry - ptr, pEntry->addr);
796 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
798 Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_DISCARD,
799 (char *)pEntry - ptr, pEntry->flags);
801 pEntry->flags = (LMEM_DISCARDED >> 8);
804 table = *(WORD *)pEntry;
806 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
810 /***********************************************************************
811 * LOCAL_FindFreeBlock
813 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
815 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
816 LOCALHEAPINFO *pInfo;
820 if (!(pInfo = LOCAL_GetHeap( ds )))
822 ERR("Local heap not found\n" );
827 arena = pInfo->first;
828 pArena = ARENA_PTR( ptr, arena );
830 arena = pArena->free_next;
831 pArena = ARENA_PTR( ptr, arena );
832 if (arena == pArena->free_next) break;
833 if (pArena->size >= size) return arena;
835 TRACE("not enough space\n" );
840 /***********************************************************************
842 * The segment may get moved around in this function, so all callers
843 * should reset their pointer variables.
845 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
847 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
848 LOCALHEAPINFO *pInfo;
852 if (!(pInfo = LOCAL_GetHeap( ds )))
854 ERR("Local heap not found\n");
859 size += ARENA_HEADER_SIZE;
860 size = LALIGN( MAX( size, sizeof(LOCALARENA) ) );
865 /* Find a suitable free block */
866 arena = LOCAL_FindFreeBlock( ds, size );
868 /* no space: try to make some */
869 LOCAL_Compact( ds, size, flags );
870 arena = LOCAL_FindFreeBlock( ds, size );
873 /* still no space: try to grow the segment */
874 if (!(LOCAL_GrowHeap( ds )))
877 /* FIXME: doesn't work correctly yet */
878 if ((pInfo->notify) && (Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_OUTOFMEM, ds - 20, size))) /* FIXME: "size" correct ? (should indicate bytes needed) */
883 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
884 pInfo = LOCAL_GetHeap( ds );
885 arena = LOCAL_FindFreeBlock( ds, size );
888 if (ds == GDI_HeapSel) {
889 ERR("not enough space in GDI local heap "
890 "(%04x) for %d bytes\n", ds, size );
891 } else if (ds == USER_HeapSel) {
892 ERR("not enough space in USER local heap "
893 "(%04x) for %d bytes\n", ds, size );
895 ERR("not enough space in local heap "
896 "%04x for %d bytes\n", ds, size );
899 if ((pInfo->notify) &&
900 /* FIXME: "size" correct ? (should indicate bytes needed) */
901 (Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_OUTOFMEM, ds, size)))
907 /* Make a block out of the free arena */
908 pArena = ARENA_PTR( ptr, arena );
909 TRACE("size = %04x, arena %04x size %04x\n",
910 size, arena, pArena->size );
911 LOCAL_RemoveFreeBlock( ptr, arena );
912 LOCAL_ShrinkArena( ds, arena, size );
914 if (flags & LMEM_ZEROINIT)
915 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
916 return arena + ARENA_HEADER_SIZE;
920 /***********************************************************************
923 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
925 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
926 LOCALHEAPINFO *pInfo;
927 LOCALHANDLEENTRY *pEntry;
932 if (!(pInfo = LOCAL_GetHeap( ds )))
934 ERR("Local heap not found\n");
939 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
940 + 2 * sizeof(WORD), LMEM_FIXED )))
942 if (!(ptr = PTR_SEG_OFF_TO_LIN( ds, 0 )))
943 ERR("ptr == NULL after GetBlock.\n");
944 if (!(pInfo = LOCAL_GetHeap( ds )))
945 ERR("pInfo == NULL after GetBlock.\n");
947 /* Fill the entry table */
949 *(WORD *)(ptr + handle) = pInfo->hdelta;
950 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
951 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
952 pEntry->lock = pEntry->flags = 0xff;
955 *(WORD *)pEntry = pInfo->htable;
956 pInfo->htable = handle;
961 /***********************************************************************
962 * LOCAL_GetNewHandleEntry
964 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
966 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
967 LOCALHEAPINFO *pInfo;
968 LOCALHANDLEENTRY *pEntry = NULL;
971 if (!(pInfo = LOCAL_GetHeap( ds )))
973 ERR("Local heap not found\n");
978 /* Find a free slot in existing tables */
980 table = pInfo->htable;
983 WORD count = *(WORD *)(ptr + table);
984 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
985 for (; count > 0; count--, pEntry++)
986 if (pEntry->lock == 0xff) break;
988 table = *(WORD *)pEntry;
991 if (!table) /* We need to create a new table */
993 if (!LOCAL_NewHTable( ds )) return 0;
994 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
995 pInfo = LOCAL_GetHeap( ds );
996 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
999 /* Now allocate this entry */
1003 TRACE("(%04x): %04x\n",
1004 ds, ((char *)pEntry - ptr) );
1005 return (HLOCAL16)((char *)pEntry - ptr);
1009 /***********************************************************************
1010 * LOCAL_FreeHandleEntry
1012 * Free a handle table entry.
1014 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1016 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1017 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1018 LOCALHEAPINFO *pInfo;
1020 WORD table, count, i;
1022 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1024 /* Find the table where this handle comes from */
1026 pTable = &pInfo->htable;
1029 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1030 if ((handle >= *pTable + sizeof(WORD)) &&
1031 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1032 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1036 ERR("Invalid entry %04x\n", handle);
1037 LOCAL_PrintHeap( ds );
1041 /* Make the entry free */
1043 pEntry->addr = 0; /* just in case */
1044 pEntry->lock = 0xff;
1045 pEntry->flags = 0xff;
1046 /* Now check if all entries in this table are free */
1049 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1050 count = *(WORD *)(ptr + table);
1051 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1053 /* Remove the table from the linked list and free it */
1055 TRACE("(%04x): freeing table %04x\n",
1057 *pTable = *(WORD *)pEntry;
1058 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1062 /***********************************************************************
1065 * Implementation of LocalFree().
1067 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1069 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1071 TRACE("%04x ds=%04x\n", handle, ds );
1073 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1074 if (HANDLE_FIXED( handle ))
1076 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1077 else return handle; /* couldn't free it */
1081 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1082 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1084 TRACE("real block at %04x\n",
1086 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr) ))
1087 return handle; /* couldn't free it */
1089 LOCAL_FreeHandleEntry( ds, handle );
1095 /***********************************************************************
1098 * Implementation of LocalAlloc().
1101 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1106 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1108 if(size > 0 && size <= 4) size = 5;
1109 if (flags & LMEM_MOVEABLE)
1111 LOCALHANDLEENTRY *plhe;
1116 if (!(hmem = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags )))
1119 else /* We just need to allocate a discarded handle */
1121 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1123 WARN("Couldn't get handle.\n");
1125 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1128 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1129 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1133 plhe->addr = hmem + sizeof(HLOCAL16);
1134 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1135 *(HLOCAL16 *)(ptr + hmem) = handle;
1140 plhe->flags = LMEM_DISCARDED >> 8;
1147 handle = LOCAL_GetBlock( ds, size, flags );
1153 /***********************************************************************
1156 * Implementation of LocalReAlloc().
1158 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1160 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1161 LOCALHEAPINFO *pInfo;
1162 LOCALARENA *pArena, *pNext;
1163 LOCALHANDLEENTRY *pEntry = NULL;
1164 WORD arena, oldsize;
1165 HLOCAL16 hmem, blockhandle;
1168 if (!handle) return 0;
1169 if(HANDLE_MOVEABLE(handle) &&
1170 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1173 TRACE("%04x %d %04x ds=%04x\n",
1174 handle, size, flags, ds );
1175 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1177 if (HANDLE_FIXED( handle ))
1178 blockhandle = handle;
1181 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1182 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1186 WARN("Dicarded block has non-zero addr.\n");
1187 TRACE("ReAllocating discarded block\n");
1188 if(size <= 4) size = 5;
1189 if (!(hl = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags)))
1191 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1192 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1193 pEntry->addr = hl + sizeof(HLOCAL16);
1196 *(HLOCAL16 *)(ptr + hl) = handle;
1199 if (((blockhandle = pEntry->addr) & 3) != 2)
1201 ERR("(%04x,%04x): invalid handle\n",
1205 if(*((HLOCAL16 *)(ptr + blockhandle) - 1) != handle) {
1206 ERR("Back ptr to handle is invalid\n");
1211 if (flags & LMEM_MODIFY)
1213 if (HANDLE_MOVEABLE(handle))
1215 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1216 pEntry->flags = (flags & 0x0f00) >> 8;
1217 TRACE("Changing flags to %x.\n", pEntry->flags);
1224 if (flags & LMEM_MOVEABLE)
1226 if (HANDLE_FIXED(handle))
1228 TRACE("Freeing fixed block.\n");
1229 return LOCAL_Free( ds, handle );
1231 else /* Moveable block */
1233 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1234 if (pEntry->lock == 0)
1236 /* discards moveable blocks */
1237 TRACE("Discarding block\n");
1238 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
1240 pEntry->flags = (LMEM_DISCARDED >> 8);
1248 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1249 if (pEntry->lock == 0)
1252 return LOCAL_Free( ds, handle );
1258 arena = ARENA_HEADER( blockhandle );
1259 TRACE("arena is %04x\n", arena );
1260 pArena = ARENA_PTR( ptr, arena );
1262 if(size <= 4) size = 5;
1263 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1264 nextarena = LALIGN(blockhandle + size);
1266 /* Check for size reduction */
1268 if (nextarena <= pArena->next)
1270 TRACE("size reduction, making new free block\n");
1271 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1272 TRACE("returning %04x\n", handle );
1276 /* Check if the next block is free and large enough */
1278 pNext = ARENA_PTR( ptr, pArena->next );
1279 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1280 (nextarena <= pNext->next))
1282 TRACE("size increase, making new free block\n");
1283 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1284 TRACE("returning %04x\n", handle );
1288 /* Now we have to allocate a new block, but not if (fixed block or locked
1289 block) and no LMEM_MOVEABLE */
1291 if (!(flags & LMEM_MOVEABLE))
1293 if (HANDLE_FIXED(handle))
1295 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1300 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1302 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1307 if(HANDLE_MOVEABLE(handle)) size += sizeof(HLOCAL16);
1308 hmem = LOCAL_GetBlock( ds, size, flags );
1309 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1310 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1311 { /* a compaction, which might in turn have */
1312 blockhandle = pEntry->addr ; /* moved the very block we are resizing */
1313 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1317 /* Remove the block from the heap and try again */
1318 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1319 if (!buffer) return 0;
1320 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1321 LOCAL_FreeArena( ds, arena );
1322 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1324 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1326 ERR("Can't restore saved block\n" );
1327 HeapFree( GetProcessHeap(), 0, buffer );
1332 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1333 memcpy( ptr + hmem, buffer, oldsize );
1334 HeapFree( GetProcessHeap(), 0, buffer );
1338 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1339 LOCAL_FreeArena( ds, arena );
1341 if (HANDLE_MOVEABLE( handle ))
1343 TRACE("fixing handle\n");
1344 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1345 pEntry->addr = hmem + sizeof(HLOCAL16);
1346 /* Back ptr should still be correct */
1347 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1348 ERR("back ptr is invalid.\n");
1351 if (size == oldsize) hmem = 0; /* Realloc failed */
1352 TRACE("returning %04x\n", hmem );
1357 /***********************************************************************
1358 * LOCAL_InternalLock
1360 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1362 HLOCAL16 old_handle = handle;
1364 if (HANDLE_MOVEABLE(handle))
1366 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1367 if (pEntry->flags == LMEM_DISCARDED) return 0;
1368 if (pEntry->lock < 0xfe) pEntry->lock++;
1369 handle = pEntry->addr;
1371 TRACE("%04x returning %04x\n",
1372 old_handle, handle );
1377 /***********************************************************************
1380 LPSTR LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1382 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1383 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1387 /***********************************************************************
1390 SEGPTR LOCAL_LockSegptr( HANDLE16 ds, HLOCAL16 handle )
1392 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1393 return PTR_SEG_OFF_TO_SEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1397 /***********************************************************************
1400 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1402 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1404 TRACE("%04x\n", handle );
1405 if (HANDLE_MOVEABLE(handle))
1407 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1408 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1409 /* For moveable block, return the new lock count */
1410 /* (see _Windows_Internals_ p. 197) */
1411 return --pEntry->lock;
1417 /***********************************************************************
1420 * Implementation of LocalSize().
1422 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1424 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1427 TRACE("%04x ds=%04x\n", handle, ds );
1429 if (HANDLE_MOVEABLE( handle )) handle = *(WORD *)(ptr + handle);
1430 if (!handle) return 0;
1431 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1432 return pArena->next - handle;
1436 /***********************************************************************
1439 * Implementation of LocalFlags().
1441 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1443 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1445 if (HANDLE_MOVEABLE(handle))
1447 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1448 TRACE("(%04x,%04x): returning %04x\n",
1449 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1450 return pEntry->lock | (pEntry->flags << 8);
1454 TRACE("(%04x,%04x): returning 0\n",
1461 /***********************************************************************
1464 * Implementation of LocalHeapSize().
1466 WORD LOCAL_HeapSize( HANDLE16 ds )
1468 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1469 if (!pInfo) return 0;
1470 return pInfo->last - pInfo->first;
1474 /***********************************************************************
1477 * Implementation of LocalCountFree().
1479 WORD LOCAL_CountFree( HANDLE16 ds )
1483 LOCALHEAPINFO *pInfo;
1484 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1486 if (!(pInfo = LOCAL_GetHeap( ds )))
1488 ERR("(%04x): Local heap not found\n", ds );
1489 LOCAL_PrintHeap( ds );
1494 arena = pInfo->first;
1495 pArena = ARENA_PTR( ptr, arena );
1498 arena = pArena->free_next;
1499 pArena = ARENA_PTR( ptr, arena );
1500 if (arena == pArena->free_next) break;
1501 total += pArena->size;
1503 TRACE("(%04x): returning %d\n", ds, total);
1508 /***********************************************************************
1511 * Implementation of LocalHandle().
1513 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1515 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1516 LOCALHEAPINFO *pInfo;
1519 if (!(pInfo = LOCAL_GetHeap( ds )))
1521 ERR("(%04x): Local heap not found\n", ds );
1522 LOCAL_PrintHeap( ds );
1526 /* Find the address in the entry tables */
1528 table = pInfo->htable;
1531 WORD count = *(WORD *)(ptr + table);
1532 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1533 for (; count > 0; count--, pEntry++)
1534 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1535 table = *(WORD *)pEntry;
1538 return (HLOCAL16)addr; /* Fixed block handle is addr */
1542 /***********************************************************************
1543 * LocalAlloc16 (KERNEL.5)
1545 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1547 HLOCAL16 ret = LOCAL_Alloc( CURRENT_DS, flags, size );
1548 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
1553 /***********************************************************************
1554 * LocalReAlloc16 (KERNEL.6)
1556 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1558 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1562 /***********************************************************************
1563 * LocalFree16 (KERNEL.7)
1565 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1567 return LOCAL_Free( CURRENT_DS, handle );
1571 /***********************************************************************
1572 * LocalLock16 (KERNEL.8)
1574 * Note: only the offset part of the pointer is returned by the relay code.
1576 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1578 return LOCAL_LockSegptr( CURRENT_DS, handle );
1582 /***********************************************************************
1583 * LocalUnlock16 (KERNEL.9)
1585 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1587 return LOCAL_Unlock( CURRENT_DS, handle );
1591 /***********************************************************************
1592 * LocalSize16 (KERNEL.10)
1594 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1596 return LOCAL_Size( CURRENT_DS, handle );
1600 /***********************************************************************
1601 * LocalHandle16 (KERNEL.11)
1603 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1605 return LOCAL_Handle( CURRENT_DS, addr );
1609 /***********************************************************************
1610 * LocalFlags16 (KERNEL.12)
1612 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1614 return LOCAL_Flags( CURRENT_DS, handle );
1618 /***********************************************************************
1619 * LocalCompact16 (KERNEL.13)
1621 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1623 TRACE("%04x\n", minfree );
1624 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1628 /***********************************************************************
1629 * LocalNotify (KERNEL.14)
1631 * Installs a callback function that is called for local memory events
1632 * Callback function prototype is
1633 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1636 * NotifyFunc seems to be responsible for allocating some memory,
1637 * returns TRUE for success.
1638 * wArg = number of bytes needed additionally
1640 * hMem = handle; wArg = old mem location
1642 * NotifyFunc seems to be strongly encouraged to return TRUE,
1643 * otherwise LogError() gets called.
1644 * hMem = handle; wArg = flags
1646 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1648 LOCALHEAPINFO *pInfo;
1649 FARPROC16 oldNotify;
1650 HANDLE16 ds = CURRENT_DS;
1652 if (!(pInfo = LOCAL_GetHeap( ds )))
1654 ERR("(%04x): Local heap not found\n", ds );
1655 LOCAL_PrintHeap( ds );
1658 TRACE("(%04x): %08lx\n", ds, (DWORD)func );
1659 FIXME("Half implemented\n");
1660 oldNotify = pInfo->notify;
1661 pInfo->notify = func;
1666 /***********************************************************************
1667 * LocalShrink16 (KERNEL.121)
1669 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1671 TRACE("%04x %04x\n", handle, newsize );
1676 /***********************************************************************
1677 * GetHeapSpaces (KERNEL.138)
1679 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1684 if (!(pModule = NE_GetPtr( module ))) return 0;
1686 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1687 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1691 /***********************************************************************
1692 * LocalCountFree (KERNEL.161)
1694 WORD WINAPI LocalCountFree16(void)
1696 return LOCAL_CountFree( CURRENT_DS );
1700 /***********************************************************************
1701 * LocalHeapSize (KERNEL.162)
1703 WORD WINAPI LocalHeapSize16(void)
1706 return LOCAL_HeapSize( CURRENT_DS );
1710 /***********************************************************************
1711 * LocalHandleDelta (KERNEL.310)
1713 WORD WINAPI LocalHandleDelta16( WORD delta )
1715 LOCALHEAPINFO *pInfo;
1717 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1719 ERR("Local heap not found\n");
1720 LOCAL_PrintHeap( CURRENT_DS );
1723 if (delta) pInfo->hdelta = delta;
1724 TRACE("returning %04x\n", pInfo->hdelta);
1725 return pInfo->hdelta;
1729 /***********************************************************************
1730 * LocalInfo (TOOLHELP.56)
1732 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1734 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(WIN16_GlobalLock16(handle)));
1735 if (!pInfo) return FALSE;
1736 pLocalInfo->wcItems = pInfo->items;
1741 /***********************************************************************
1742 * LocalFirst (TOOLHELP.57)
1744 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1746 WORD ds = GlobalHandleToSel16( handle );
1747 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1748 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1749 if (!pInfo) return FALSE;
1751 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1752 pLocalEntry->wAddress = pLocalEntry->hHandle;
1753 pLocalEntry->wFlags = LF_FIXED;
1754 pLocalEntry->wcLock = 0;
1755 pLocalEntry->wType = LT_NORMAL;
1756 pLocalEntry->hHeap = handle;
1757 pLocalEntry->wHeapType = NORMAL_HEAP;
1758 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1759 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1764 /***********************************************************************
1765 * LocalNext (TOOLHELP.58)
1767 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1769 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1770 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1773 if (!LOCAL_GetHeap( ds )) return FALSE;
1774 if (!pLocalEntry->wNext) return FALSE;
1775 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1777 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1778 pLocalEntry->wAddress = pLocalEntry->hHandle;
1779 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1780 pLocalEntry->wcLock = 0;
1781 pLocalEntry->wType = LT_NORMAL;
1782 if (pArena->next != pLocalEntry->wNext) /* last one? */
1783 pLocalEntry->wNext = pArena->next;
1785 pLocalEntry->wNext = 0;
1786 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1791 /***********************************************************************
1792 * LocalAlloc32 (KERNEL32.371)
1797 HLOCAL WINAPI LocalAlloc(
1798 UINT flags, /* [in] Allocation attributes */
1799 DWORD size /* [in] Number of bytes to allocate */
1801 return (HLOCAL)GlobalAlloc( flags, size );
1805 /***********************************************************************
1806 * LocalCompact32 (KERNEL32.372)
1808 UINT WINAPI LocalCompact( UINT minfree )
1810 return 0; /* LocalCompact does nothing in Win32 */
1814 /***********************************************************************
1815 * LocalFlags32 (KERNEL32.374)
1817 * Value specifying allocation flags and lock count.
1818 * LMEM_INVALID_HANDLE: Failure
1820 UINT WINAPI LocalFlags(
1821 HLOCAL handle /* [in] Handle of memory object */
1823 return GlobalFlags( (HGLOBAL)handle );
1827 /***********************************************************************
1828 * LocalFree32 (KERNEL32.375)
1833 HLOCAL WINAPI LocalFree(
1834 HLOCAL handle /* [in] Handle of memory object */
1836 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
1840 /***********************************************************************
1841 * LocalHandle32 (KERNEL32.376)
1846 HLOCAL WINAPI LocalHandle(
1847 LPCVOID ptr /* [in] Address of local memory object */
1849 return (HLOCAL)GlobalHandle( ptr );
1853 /***********************************************************************
1854 * LocalLock32 (KERNEL32.377)
1855 * Locks a local memory object and returns pointer to the first byte
1856 * of the memory block.
1862 LPVOID WINAPI LocalLock(
1863 HLOCAL handle /* [in] Address of local memory object */
1865 return GlobalLock( (HGLOBAL)handle );
1869 /***********************************************************************
1870 * LocalReAlloc32 (KERNEL32.378)
1875 HLOCAL WINAPI LocalReAlloc(
1876 HLOCAL handle, /* [in] Handle of memory object */
1877 DWORD size, /* [in] New size of block */
1878 UINT flags /* [in] How to reallocate object */
1880 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1884 /***********************************************************************
1885 * LocalShrink32 (KERNEL32.379)
1887 UINT WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1889 return 0; /* LocalShrink does nothing in Win32 */
1893 /***********************************************************************
1894 * LocalSize32 (KERNEL32.380)
1899 UINT WINAPI LocalSize(
1900 HLOCAL handle /* [in] Handle of memory object */
1902 return GlobalSize( (HGLOBAL)handle );
1906 /***********************************************************************
1907 * LocalUnlock32 (KERNEL32.381)
1909 * TRUE: Object is still locked
1910 * FALSE: Object is unlocked
1912 BOOL WINAPI LocalUnlock(
1913 HLOCAL handle /* [in] Handle of memory object */
1915 return GlobalUnlock( (HGLOBAL)handle );