msi: Adjust the signedness of three variables.
[wine/wine64.git] / dlls / kernel32 / local16.c
blobe250d84c4da1e9c7790ff85ff1662940916c8e6d
1 /*
2 * 16-bit local heap functions
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Huw Davies
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Note:
25 * All local heap functions need the current DS as first parameter
26 * when called from the emulation library, so they take one more
27 * parameter than usual.
30 #include "config.h"
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include <stdlib.h>
35 #include <string.h>
36 #include "wine/winbase16.h"
37 #include "wownt32.h"
38 #include "toolhelp.h"
39 #include "winternl.h"
40 #include "kernel_private.h"
41 #include "kernel16_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(local);
46 typedef struct
48 /* Arena header */
49 WORD prev; /* Previous arena | arena type */
50 WORD next; /* Next arena */
51 /* Start of the memory block or free-list info */
52 WORD size; /* Size of the free block */
53 WORD free_prev; /* Previous free block */
54 WORD free_next; /* Next free block */
55 } LOCALARENA;
57 #define ARENA_HEADER_SIZE 4
58 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
60 /* Arena types (stored in 'prev' field of the arena) */
61 #define LOCAL_ARENA_FREE 0
62 #define LOCAL_ARENA_FIXED 1
64 /* LocalNotify() msgs */
66 #define LN_OUTOFMEM 0
67 #define LN_MOVE 1
68 #define LN_DISCARD 2
70 /* Layout of a handle entry table
72 * WORD count of entries
73 * LOCALHANDLEENTRY[count] entries
74 * WORD near ptr to next table
76 typedef struct
78 WORD addr; /* Address of the MOVEABLE block */
79 BYTE flags; /* Flags for this block */
80 BYTE lock; /* Lock count */
81 } LOCALHANDLEENTRY;
84 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
85 * in case something actually relies on this.
86 * Note that if the architecture does not allow unaligned accesses, we make
87 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
89 * An unused handle has lock = flags = 0xff. In windows addr is that of next
90 * free handle, at the moment in wine we set it to 0.
92 * A discarded block's handle has lock = addr = 0 and flags = 0x40
93 * (LMEM_DISCARDED >> 8)
96 #ifdef ALLOW_UNALIGNED_ACCESS
97 # define MOVEABLE_PREFIX sizeof(HLOCAL16)
98 #else
99 # define MOVEABLE_PREFIX sizeof(int)
100 #endif
103 #include "pshpack1.h"
105 typedef struct
107 WORD check; /* 00 Heap checking flag */
108 WORD freeze; /* 02 Heap frozen flag */
109 WORD items; /* 04 Count of items on the heap */
110 WORD first; /* 06 First item of the heap */
111 WORD pad1; /* 08 Always 0 */
112 WORD last; /* 0a Last item of the heap */
113 WORD pad2; /* 0c Always 0 */
114 BYTE ncompact; /* 0e Compactions counter */
115 BYTE dislevel; /* 0f Discard level */
116 DWORD distotal; /* 10 Total bytes discarded */
117 WORD htable; /* 14 Pointer to handle table */
118 WORD hfree; /* 16 Pointer to free handle table */
119 WORD hdelta; /* 18 Delta to expand the handle table */
120 WORD expand; /* 1a Pointer to expand function (unused) */
121 WORD pstat; /* 1c Pointer to status structure (unused) */
122 FARPROC16 notify; /* 1e Pointer to LocalNotify() function */
123 WORD lock; /* 22 Lock count for the heap */
124 WORD extra; /* 24 Extra bytes to allocate when expanding */
125 WORD minsize; /* 26 Minimum size of the heap */
126 WORD magic; /* 28 Magic number */
127 } LOCALHEAPINFO;
129 #include "poppack.h"
131 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
133 /* All local heap allocations are aligned on 4-byte boundaries */
134 #define LALIGN(word) (((word) + 3) & ~3)
136 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char *)(ptr)+(arena)))
137 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
138 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
139 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
141 /* determine whether the handle belongs to a fixed or a moveable block */
142 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
143 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
146 /* 32-bit heap definitions */
148 #define HTABLE_SIZE 0x10000
149 #define HTABLE_PAGESIZE 0x1000
150 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
152 #include "pshpack1.h"
153 typedef struct _LOCAL32HEADER
155 WORD freeListFirst[HTABLE_NPAGES];
156 WORD freeListSize[HTABLE_NPAGES];
157 WORD freeListLast[HTABLE_NPAGES];
159 DWORD selectorTableOffset;
160 WORD selectorTableSize;
161 WORD selectorDelta;
163 DWORD segment;
164 LPBYTE base;
166 DWORD limit;
167 DWORD flags;
169 DWORD magic;
170 HANDLE heap;
172 } LOCAL32HEADER;
173 #include "poppack.h"
175 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
178 static inline BOOL16 call_notify_func( FARPROC16 proc, WORD msg, HLOCAL16 handle, WORD arg )
180 DWORD ret;
181 WORD args[3];
183 if (!proc) return FALSE;
184 args[2] = msg;
185 args[1] = handle;
186 args[0] = arg;
187 WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
188 return LOWORD(ret);
192 /***********************************************************************
193 * LOCAL_GetHeap
195 * Return a pointer to the local heap, making sure it exists.
197 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
199 LOCALHEAPINFO *pInfo;
200 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
201 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
202 if (!ptr || !ptr->heap) return NULL;
203 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
205 WARN("Bad pointer\n");
206 return NULL;
208 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
209 if (pInfo->magic != LOCAL_HEAP_MAGIC)
211 WARN("Bad magic\n");
212 return NULL;
214 return pInfo;
218 /***********************************************************************
219 * LOCAL_MakeBlockFree
221 * Make a block free, inserting it in the free-list.
222 * 'block' is the handle of the block arena; 'baseptr' points to
223 * the beginning of the data segment containing the heap.
225 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
227 LOCALARENA *pArena, *pNext;
228 WORD next;
230 /* Mark the block as free */
232 pArena = ARENA_PTR( baseptr, block );
233 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
234 pArena->size = pArena->next - block;
236 /* Find the next free block (last block is always free) */
238 next = pArena->next;
239 for (;;)
241 pNext = ARENA_PTR( baseptr, next );
242 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
243 next = pNext->next;
246 TRACE("%04x, next %04x\n", block, next );
247 /* Insert the free block in the free-list */
249 pArena->free_prev = pNext->free_prev;
250 pArena->free_next = next;
251 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
252 pNext->free_prev = block;
256 /***********************************************************************
257 * LOCAL_RemoveFreeBlock
259 * Remove a block from the free-list.
260 * 'block' is the handle of the block arena; 'baseptr' points to
261 * the beginning of the data segment containing the heap.
263 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
265 /* Mark the block as fixed */
267 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
268 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
270 /* Remove it from the list */
272 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
273 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
277 /***********************************************************************
278 * LOCAL_AddBlock
280 * Insert a new block in the heap.
281 * 'new' is the handle of the new block arena; 'baseptr' points to
282 * the beginning of the data segment containing the heap; 'prev' is
283 * the block before the new one.
285 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
287 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
288 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
290 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
291 pNew->next = pPrev->next;
292 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
293 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
294 pPrev->next = new;
298 /***********************************************************************
299 * LOCAL_RemoveBlock
301 * Remove a block from the heap.
302 * 'block' is the handle of the block arena; 'baseptr' points to
303 * the beginning of the data segment containing the heap.
305 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
307 LOCALARENA *pArena, *pTmp;
309 /* Remove the block from the free-list */
311 TRACE("\n");
312 pArena = ARENA_PTR( baseptr, block );
313 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
314 LOCAL_RemoveFreeBlock( baseptr, block );
316 /* If the previous block is free, expand its size */
318 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
319 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
320 pTmp->size += pArena->next - block;
322 /* Remove the block from the linked list */
324 pTmp->next = pArena->next;
325 pTmp = ARENA_PTR( baseptr, pArena->next );
326 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
330 /***********************************************************************
331 * LOCAL_PrintHeap
333 static void LOCAL_PrintHeap( HANDLE16 ds )
335 char *ptr;
336 LOCALHEAPINFO *pInfo;
337 WORD arena;
339 /* FIXME - the test should be done when calling the function!
340 plus is not clear that we should print this info
341 only when TRACE_ON is on! */
342 if(!TRACE_ON(local)) return;
344 ptr = MapSL( MAKESEGPTR( ds, 0 ));
345 pInfo = LOCAL_GetHeap( ds );
347 if (!pInfo)
349 ERR( "Local Heap corrupted! ds=%04x\n", ds );
350 return;
352 TRACE( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
353 ds, pInfo->first, pInfo->last, pInfo->items );
355 arena = pInfo->first;
356 for (;;)
358 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
359 TRACE( " %04x: prev=%04x next=%04x type=%d\n", arena,
360 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
361 if (arena == pInfo->first)
363 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
364 pArena->size, pArena->free_prev, pArena->free_next );
366 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
368 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
369 pArena->size, pArena->free_prev, pArena->free_next );
370 if (pArena->next == arena) break; /* last one */
371 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
373 TRACE( "*** arena->free_next->free_prev != arena\n" );
374 break;
377 if (pArena->next == arena)
379 TRACE( "*** last block is not marked free\n" );
380 break;
382 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
384 TRACE( "*** arena->next->prev != arena (%04x, %04x)\n",
385 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
386 break;
388 arena = pArena->next;
393 /***********************************************************************
394 * LocalInit (KERNEL.4)
396 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
398 char *ptr;
399 WORD heapInfoArena, freeArena, lastArena;
400 LOCALHEAPINFO *pHeapInfo;
401 LOCALARENA *pArena, *pFirstArena, *pLastArena;
402 BOOL16 ret = FALSE;
404 /* The initial layout of the heap is: */
405 /* - first arena (FIXED) */
406 /* - heap info structure (FIXED) */
407 /* - large free block (FREE) */
408 /* - last arena (FREE) */
410 TRACE("%04x %04x-%04x\n", selector, start, end);
411 if (!selector) selector = CURRENT_DS;
413 if (TRACE_ON(local))
415 /* If TRACE_ON(heap) is set, the global heap blocks are */
416 /* cleared before use, so we can test for double initialization. */
417 if (LOCAL_GetHeap(selector))
419 ERR("Heap %04x initialized twice.\n", selector);
420 LOCAL_PrintHeap(selector);
424 if (start == 0)
426 /* start == 0 means: put the local heap at the end of the segment */
428 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
429 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
430 if ( end > 0xfffe ) end = 0xfffe;
431 start -= end;
432 end += start;
434 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
436 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
437 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
438 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
439 + sizeof(LOCALHEAPINFO) );
440 lastArena = (end - sizeof(LOCALARENA)) & ~3;
442 /* Make sure there's enough space. */
444 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
446 /* Initialise the first arena */
448 pFirstArena = ARENA_PTR( ptr, start );
449 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
450 pFirstArena->next = heapInfoArena;
451 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
452 pFirstArena->free_prev = start; /* this one */
453 pFirstArena->free_next = freeArena;
455 /* Initialise the arena of the heap info structure */
457 pArena = ARENA_PTR( ptr, heapInfoArena );
458 pArena->prev = start | LOCAL_ARENA_FIXED;
459 pArena->next = freeArena;
461 /* Initialise the heap info structure */
463 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
464 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
465 pHeapInfo->items = 4;
466 pHeapInfo->first = start;
467 pHeapInfo->last = lastArena;
468 pHeapInfo->htable = 0;
469 pHeapInfo->hdelta = 0x20;
470 pHeapInfo->extra = 0x200;
471 pHeapInfo->minsize = lastArena - freeArena;
472 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
474 /* Initialise the large free block */
476 pArena = ARENA_PTR( ptr, freeArena );
477 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
478 pArena->next = lastArena;
479 pArena->size = lastArena - freeArena;
480 pArena->free_prev = start;
481 pArena->free_next = lastArena;
483 /* Initialise the last block */
485 pLastArena = ARENA_PTR( ptr, lastArena );
486 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
487 pLastArena->next = lastArena; /* this one */
488 pLastArena->size = LALIGN(sizeof(LOCALARENA));
489 pLastArena->free_prev = freeArena;
490 pLastArena->free_next = lastArena; /* this one */
492 /* Store the local heap address in the instance data */
494 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
495 LOCAL_PrintHeap( selector );
496 ret = TRUE;
498 done:
499 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
500 return ret;
504 /***********************************************************************
505 * LOCAL_GrowHeap
507 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
509 HANDLE16 hseg;
510 LONG oldsize;
511 LONG end;
512 LOCALHEAPINFO *pHeapInfo;
513 WORD freeArena, lastArena;
514 LOCALARENA *pArena, *pLastArena;
515 char *ptr;
517 hseg = GlobalHandle16( ds );
518 /* maybe mem allocated by Virtual*() ? */
519 if (!hseg) return FALSE;
521 oldsize = GlobalSize16( hseg );
522 /* if nothing can be gained, return */
523 if (oldsize > 0xfff0) return FALSE;
524 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
525 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
526 pHeapInfo = LOCAL_GetHeap( ds );
527 if (pHeapInfo == NULL) {
528 ERR("Heap not found\n" );
529 return FALSE;
531 end = GlobalSize16( hseg );
532 lastArena = (end - sizeof(LOCALARENA)) & ~3;
534 /* Update the HeapInfo */
535 pHeapInfo->items++;
536 freeArena = pHeapInfo->last;
537 pHeapInfo->last = lastArena;
538 pHeapInfo->minsize += end - oldsize;
540 /* grow the old last block */
541 pArena = ARENA_PTR( ptr, freeArena );
542 pArena->size = lastArena - freeArena;
543 pArena->next = lastArena;
544 pArena->free_next = lastArena;
546 /* Initialise the new last block */
548 pLastArena = ARENA_PTR( ptr, lastArena );
549 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
550 pLastArena->next = lastArena; /* this one */
551 pLastArena->size = LALIGN(sizeof(LOCALARENA));
552 pLastArena->free_prev = freeArena;
553 pLastArena->free_next = lastArena; /* this one */
555 /* If block before freeArena is also free then merge them */
556 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
558 LOCAL_RemoveBlock(ptr, freeArena);
559 pHeapInfo->items--;
562 TRACE("Heap expanded\n" );
563 LOCAL_PrintHeap( ds );
564 return TRUE;
568 /***********************************************************************
569 * LOCAL_FreeArena
571 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
573 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
574 LOCALHEAPINFO *pInfo;
575 LOCALARENA *pArena, *pPrev, *pNext;
577 TRACE("%04x ds=%04x\n", arena, ds );
578 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
580 pArena = ARENA_PTR( ptr, arena );
581 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
583 /* shouldn't happen */
584 ERR("Trying to free block %04x twice!\n",
585 arena );
586 LOCAL_PrintHeap( ds );
587 return arena;
590 /* Check if we can merge with the previous block */
592 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
593 pNext = ARENA_PTR( ptr, pArena->next );
594 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
596 arena = pArena->prev & ~3;
597 pArena = pPrev;
598 LOCAL_RemoveBlock( ptr, pPrev->next );
599 pInfo->items--;
601 else /* Make a new free block */
603 LOCAL_MakeBlockFree( ptr, arena );
606 /* Check if we can merge with the next block */
608 if ((pArena->next == pArena->free_next) &&
609 (pArena->next != pInfo->last))
611 LOCAL_RemoveBlock( ptr, pArena->next );
612 pInfo->items--;
614 return 0;
618 /***********************************************************************
619 * LOCAL_ShrinkArena
621 * Shrink an arena by creating a free block at its end if possible.
622 * 'size' includes the arena header, and must be aligned.
624 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
626 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
627 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
629 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
631 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
632 if (!pInfo) return;
633 LOCAL_AddBlock( ptr, arena, arena + size );
634 pInfo->items++;
635 LOCAL_FreeArena( ds, arena + size );
640 /***********************************************************************
641 * LOCAL_GrowArenaDownward
643 * Grow an arena downward by using the previous arena (must be free).
645 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
647 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
648 LOCALHEAPINFO *pInfo;
649 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
650 WORD prevArena = pArena->prev & ~3;
651 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
652 WORD offset, size;
653 char *p;
655 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
656 offset = pPrevArena->size;
657 size = pArena->next - arena - ARENA_HEADER_SIZE;
658 LOCAL_RemoveFreeBlock( ptr, prevArena );
659 LOCAL_RemoveBlock( ptr, arena );
660 pInfo->items--;
661 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
662 while (offset < size)
664 memcpy( p, p + offset, offset );
665 p += offset;
666 size -= offset;
668 if (size) memcpy( p, p + offset, size );
669 LOCAL_ShrinkArena( ds, prevArena, newsize );
674 /***********************************************************************
675 * LOCAL_GrowArenaUpward
677 * Grow an arena upward by using the next arena (must be free and big
678 * enough). Newsize includes the arena header and must be aligned.
680 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
682 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
683 LOCALHEAPINFO *pInfo;
684 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
685 WORD nextArena = pArena->next;
687 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
688 LOCAL_RemoveBlock( ptr, nextArena );
689 pInfo->items--;
690 LOCAL_ShrinkArena( ds, arena, newsize );
694 /***********************************************************************
695 * LOCAL_GetFreeSpace
697 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
699 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
700 LOCALHEAPINFO *pInfo;
701 LOCALARENA *pArena;
702 WORD arena;
703 WORD freespace = 0;
705 if (!(pInfo = LOCAL_GetHeap( ds )))
707 ERR("Local heap not found\n" );
708 LOCAL_PrintHeap(ds);
709 return 0;
711 arena = pInfo->first;
712 pArena = ARENA_PTR( ptr, arena );
713 while (arena != pArena->free_next)
715 arena = pArena->free_next;
716 pArena = ARENA_PTR( ptr, arena );
717 if (pArena->size >= freespace) freespace = pArena->size;
719 /* FIXME doesn't yet calculate space that would become free if everything
720 were discarded when countdiscard == 1 */
721 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
722 else freespace -= ARENA_HEADER_SIZE;
723 return freespace;
727 /***********************************************************************
728 * LOCAL_Compact
730 static UINT16 LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
732 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
733 LOCALHEAPINFO *pInfo;
734 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
735 WORD arena, movearena, finalarena, table;
736 WORD count, movesize, size;
737 WORD freespace;
738 LOCALHANDLEENTRY *pEntry;
740 if (!(pInfo = LOCAL_GetHeap( ds )))
742 ERR("Local heap not found\n" );
743 LOCAL_PrintHeap(ds);
744 return 0;
746 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
747 ds, minfree, flags);
748 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
749 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
751 TRACE("Returning %04x.\n", freespace);
752 return freespace;
754 TRACE("Compacting heap %04x.\n", ds);
755 table = pInfo->htable;
756 while(table)
758 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
759 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
761 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
763 /* OK we can move this one if we want */
764 TRACE("handle %04x (block %04x) can be moved.\n",
765 (WORD)((char *)pEntry - ptr), pEntry->addr);
766 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
767 pMoveArena = ARENA_PTR(ptr, movearena);
768 movesize = pMoveArena->next - movearena;
769 arena = pInfo->first;
770 pArena = ARENA_PTR(ptr, arena);
771 size = 0xffff;
772 finalarena = 0;
773 /* Try to find the smallest arena that will do, */
774 /* which is below us in memory */
775 for(;;)
777 arena = pArena->free_next;
778 pArena = ARENA_PTR(ptr, arena);
779 if(arena >= movearena)
780 break;
781 if(arena == pArena->free_next)
782 break;
783 if((pArena->size >= movesize) && (pArena->size < size))
785 size = pArena->size;
786 finalarena = arena;
789 if (finalarena) /* Actually got somewhere to move */
791 TRACE("Moving it to %04x.\n", finalarena);
792 pFinalArena = ARENA_PTR(ptr, finalarena);
793 size = pFinalArena->size;
794 LOCAL_RemoveFreeBlock(ptr, finalarena);
795 LOCAL_ShrinkArena( ds, finalarena, movesize );
796 /* Copy the arena to it's new location */
797 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
798 (char *)pMoveArena + ARENA_HEADER_SIZE,
799 movesize - ARENA_HEADER_SIZE );
800 /* Free the old location */
801 LOCAL_FreeArena(ds, movearena);
802 call_notify_func(pInfo->notify, LN_MOVE,
803 (WORD)((char *)pEntry - ptr), pEntry->addr);
804 /* Update handle table entry */
805 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
807 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
808 == LOCAL_ARENA_FREE)
810 /* Previous arena is free (but < movesize) */
811 /* so we can 'slide' movearena down into it */
812 finalarena = pMoveArena->prev & ~3;
813 LOCAL_GrowArenaDownward( ds, movearena, movesize );
814 /* Update handle table entry */
815 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
819 table = *(WORD *)pEntry;
821 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
822 if(freespace >= minfree || (flags & LMEM_NODISCARD))
824 TRACE("Returning %04x.\n", freespace);
825 return freespace;
828 table = pInfo->htable;
829 while(table)
831 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
832 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
834 if(pEntry->addr && pEntry->lock == 0 &&
835 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
837 TRACE("Discarding handle %04x (block %04x).\n",
838 (char *)pEntry - ptr, pEntry->addr);
839 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
840 call_notify_func(pInfo->notify, LN_DISCARD, (char *)pEntry - ptr, pEntry->flags);
841 pEntry->addr = 0;
842 pEntry->flags = (LMEM_DISCARDED >> 8);
845 table = *(WORD *)pEntry;
847 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
851 /***********************************************************************
852 * LOCAL_FindFreeBlock
854 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
856 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
857 LOCALHEAPINFO *pInfo;
858 LOCALARENA *pArena;
859 WORD arena;
861 if (!(pInfo = LOCAL_GetHeap( ds )))
863 ERR("Local heap not found\n" );
864 LOCAL_PrintHeap(ds);
865 return 0;
868 arena = pInfo->first;
869 pArena = ARENA_PTR( ptr, arena );
870 for (;;) {
871 arena = pArena->free_next;
872 pArena = ARENA_PTR( ptr, arena );
873 if (arena == pArena->free_next) break;
874 if (pArena->size >= size) return arena;
876 TRACE("not enough space\n" );
877 LOCAL_PrintHeap(ds);
878 return 0;
882 /***********************************************************************
883 * get_heap_name
885 static const char *get_heap_name( WORD ds )
887 HINSTANCE16 inst = LoadLibrary16( "GDI" );
888 if (ds == GlobalHandleToSel16( inst ))
890 FreeLibrary16( inst );
891 return "GDI";
893 FreeLibrary16( inst );
894 inst = LoadLibrary16( "USER" );
895 if (ds == GlobalHandleToSel16( inst ))
897 FreeLibrary16( inst );
898 return "USER";
900 FreeLibrary16( inst );
901 return "local";
904 /***********************************************************************
905 * LOCAL_GetBlock
906 * The segment may get moved around in this function, so all callers
907 * should reset their pointer variables.
909 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
911 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
912 LOCALHEAPINFO *pInfo;
913 LOCALARENA *pArena;
914 WORD arena;
916 if (!(pInfo = LOCAL_GetHeap( ds )))
918 ERR("Local heap not found\n");
919 LOCAL_PrintHeap(ds);
920 return 0;
923 size += ARENA_HEADER_SIZE;
924 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
926 #if 0
927 notify_done:
928 #endif
929 /* Find a suitable free block */
930 arena = LOCAL_FindFreeBlock( ds, size );
931 if (arena == 0) {
932 /* no space: try to make some */
933 LOCAL_Compact( ds, size, flags );
934 arena = LOCAL_FindFreeBlock( ds, size );
936 if (arena == 0) {
937 /* still no space: try to grow the segment */
938 if (!(LOCAL_GrowHeap( ds )))
940 #if 0
941 /* FIXME: doesn't work correctly yet */
942 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds - 20, size)) /* FIXME: "size" correct ? (should indicate bytes needed) */
943 goto notify_done;
944 #endif
945 ERR( "not enough space in %s heap %04x for %d bytes\n",
946 get_heap_name(ds), ds, size );
947 return 0;
949 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
950 pInfo = LOCAL_GetHeap( ds );
951 arena = LOCAL_FindFreeBlock( ds, size );
953 if (arena == 0) {
954 ERR( "not enough space in %s heap %04x for %d bytes\n",
955 get_heap_name(ds), ds, size );
956 #if 0
957 /* FIXME: "size" correct ? (should indicate bytes needed) */
958 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds, size)) goto notify_done;
959 #endif
960 return 0;
963 /* Make a block out of the free arena */
964 pArena = ARENA_PTR( ptr, arena );
965 TRACE("size = %04x, arena %04x size %04x\n", size, arena, pArena->size );
966 LOCAL_RemoveFreeBlock( ptr, arena );
967 LOCAL_ShrinkArena( ds, arena, size );
969 if (flags & LMEM_ZEROINIT)
970 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
971 return arena + ARENA_HEADER_SIZE;
975 /***********************************************************************
976 * LOCAL_NewHTable
978 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
980 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
981 LOCALHEAPINFO *pInfo;
982 LOCALHANDLEENTRY *pEntry;
983 HLOCAL16 handle;
984 int i;
986 TRACE("\n" );
987 if (!(pInfo = LOCAL_GetHeap( ds )))
989 ERR("Local heap not found\n");
990 LOCAL_PrintHeap(ds);
991 return FALSE;
994 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
995 + 2 * sizeof(WORD), LMEM_FIXED )))
996 return FALSE;
997 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
998 ERR("ptr == NULL after GetBlock.\n");
999 if (!(pInfo = LOCAL_GetHeap( ds )))
1000 ERR("pInfo == NULL after GetBlock.\n");
1002 /* Fill the entry table */
1004 *(WORD *)(ptr + handle) = pInfo->hdelta;
1005 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
1006 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
1007 pEntry->lock = pEntry->flags = 0xff;
1008 pEntry->addr = 0;
1010 *(WORD *)pEntry = pInfo->htable;
1011 pInfo->htable = handle;
1012 return TRUE;
1016 /***********************************************************************
1017 * LOCAL_GetNewHandleEntry
1019 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
1021 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1022 LOCALHEAPINFO *pInfo;
1023 LOCALHANDLEENTRY *pEntry = NULL;
1024 WORD table;
1026 if (!(pInfo = LOCAL_GetHeap( ds )))
1028 ERR("Local heap not found\n");
1029 LOCAL_PrintHeap(ds);
1030 return 0;
1033 /* Find a free slot in existing tables */
1035 table = pInfo->htable;
1036 while (table)
1038 WORD count = *(WORD *)(ptr + table);
1039 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1040 for (; count > 0; count--, pEntry++)
1041 if (pEntry->lock == 0xff) break;
1042 if (count) break;
1043 table = *(WORD *)pEntry;
1046 if (!table) /* We need to create a new table */
1048 if (!LOCAL_NewHTable( ds )) return 0;
1049 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1050 pInfo = LOCAL_GetHeap( ds );
1051 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1054 /* Now allocate this entry */
1056 pEntry->lock = 0;
1057 pEntry->flags = 0;
1058 TRACE("(%04x): %04x\n", ds, ((char *)pEntry - ptr) );
1059 return (HLOCAL16)((char *)pEntry - ptr);
1063 /***********************************************************************
1064 * LOCAL_FreeHandleEntry
1066 * Free a handle table entry.
1068 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1070 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1071 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1072 LOCALHEAPINFO *pInfo;
1073 WORD *pTable;
1074 WORD table, count, i;
1076 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1078 /* Find the table where this handle comes from */
1080 pTable = &pInfo->htable;
1081 while (*pTable)
1083 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1084 if ((handle >= *pTable + sizeof(WORD)) &&
1085 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1086 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1088 if (!*pTable)
1090 ERR("Invalid entry %04x\n", handle);
1091 LOCAL_PrintHeap( ds );
1092 return;
1095 /* Make the entry free */
1097 pEntry->addr = 0; /* just in case */
1098 pEntry->lock = 0xff;
1099 pEntry->flags = 0xff;
1100 /* Now check if all entries in this table are free */
1102 table = *pTable;
1103 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1104 count = *(WORD *)(ptr + table);
1105 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1107 /* Remove the table from the linked list and free it */
1109 TRACE("(%04x): freeing table %04x\n", ds, table);
1110 *pTable = *(WORD *)pEntry;
1111 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1115 /***********************************************************************
1116 * LocalFree (KERNEL.7)
1118 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1120 HANDLE16 ds = CURRENT_DS;
1121 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1123 TRACE("%04x ds=%04x\n", handle, ds );
1125 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1126 if (HANDLE_FIXED( handle ))
1128 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1129 else return handle; /* couldn't free it */
1131 else
1133 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1134 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1136 TRACE("real block at %04x\n", pEntry->addr );
1137 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1138 return handle; /* couldn't free it */
1140 LOCAL_FreeHandleEntry( ds, handle );
1141 return 0; /* OK */
1146 /***********************************************************************
1147 * LocalAlloc (KERNEL.5)
1149 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1151 HANDLE16 ds = CURRENT_DS;
1152 HLOCAL16 handle = 0;
1153 char *ptr;
1155 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1157 if(size > 0 && size <= 4) size = 5;
1158 if (flags & LMEM_MOVEABLE)
1160 LOCALHANDLEENTRY *plhe;
1161 HLOCAL16 hmem;
1163 if(size)
1165 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1166 goto exit;
1168 else /* We just need to allocate a discarded handle */
1169 hmem = 0;
1170 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1172 WARN("Couldn't get handle.\n");
1173 if(hmem)
1174 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1175 goto exit;
1177 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1178 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1179 plhe->lock = 0;
1180 if(hmem)
1182 plhe->addr = hmem + MOVEABLE_PREFIX;
1183 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1184 *(HLOCAL16 *)(ptr + hmem) = handle;
1186 else
1188 plhe->addr = 0;
1189 plhe->flags = LMEM_DISCARDED >> 8;
1192 else /* FIXED */
1194 if(size) handle = LOCAL_GetBlock( ds, size, flags );
1197 exit:
1198 CURRENT_STACK16->ecx = handle; /* must be returned in cx too */
1199 return handle;
1203 /***********************************************************************
1204 * LocalReAlloc (KERNEL.6)
1206 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1208 HANDLE16 ds = CURRENT_DS;
1209 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1210 LOCALHEAPINFO *pInfo;
1211 LOCALARENA *pArena, *pNext;
1212 LOCALHANDLEENTRY *pEntry = NULL;
1213 WORD arena, oldsize;
1214 HLOCAL16 hmem, blockhandle;
1215 LONG nextarena;
1217 if (!handle) return 0;
1218 if(HANDLE_MOVEABLE(handle) &&
1219 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1220 return 0;
1222 TRACE("%04x %d %04x ds=%04x\n", handle, size, flags, ds );
1223 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1225 if (HANDLE_FIXED( handle ))
1226 blockhandle = handle;
1227 else
1229 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1230 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1232 HLOCAL16 hl;
1233 if(pEntry->addr)
1234 WARN("Dicarded block has non-zero addr.\n");
1235 TRACE("ReAllocating discarded block\n");
1236 if(size <= 4) size = 5;
1237 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1238 return 0;
1239 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1240 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1241 pEntry->addr = hl + MOVEABLE_PREFIX;
1242 pEntry->flags = 0;
1243 pEntry->lock = 0;
1244 *(HLOCAL16 *)(ptr + hl) = handle;
1245 return handle;
1247 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1249 ERR("(%04x,%04x): invalid handle\n",
1250 ds, handle );
1251 return 0;
1253 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1254 ERR("Back ptr to handle is invalid\n");
1255 return 0;
1259 if (flags & LMEM_MODIFY)
1261 if (HANDLE_MOVEABLE(handle))
1263 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1264 pEntry->flags = (flags & 0x0f00) >> 8;
1265 TRACE("Changing flags to %x.\n", pEntry->flags);
1267 return handle;
1270 if (!size)
1272 if (flags & LMEM_MOVEABLE)
1274 if (HANDLE_FIXED(handle))
1276 TRACE("Freeing fixed block.\n");
1277 return LocalFree16( handle );
1279 else /* Moveable block */
1281 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1282 if (pEntry->lock == 0)
1284 /* discards moveable blocks */
1285 TRACE("Discarding block\n");
1286 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1287 pEntry->addr = 0;
1288 pEntry->flags = (LMEM_DISCARDED >> 8);
1289 return handle;
1292 return 0;
1294 else if(flags == 0)
1296 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1297 if (pEntry->lock == 0)
1299 /* Frees block */
1300 return LocalFree16( handle );
1303 return 0;
1306 arena = ARENA_HEADER( blockhandle );
1307 TRACE("arena is %04x\n", arena );
1308 pArena = ARENA_PTR( ptr, arena );
1310 if(size <= 4) size = 5;
1311 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1312 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1313 nextarena = LALIGN(blockhandle + size);
1315 /* Check for size reduction */
1317 if (nextarena <= pArena->next)
1319 TRACE("size reduction, making new free block\n");
1320 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1321 TRACE("returning %04x\n", handle );
1322 return handle;
1325 /* Check if the next block is free and large enough */
1327 pNext = ARENA_PTR( ptr, pArena->next );
1328 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1329 (nextarena <= pNext->next))
1331 TRACE("size increase, making new free block\n");
1332 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1333 if (flags & LMEM_ZEROINIT)
1335 char *oldend = (char *)pArena + ARENA_HEADER_SIZE + oldsize;
1336 char *newend = ptr + pArena->next;
1337 TRACE("Clearing memory from %p to %p (DS -> %p)\n", oldend, newend, ptr);
1338 memset(oldend, 0, newend - oldend);
1341 TRACE("returning %04x\n", handle );
1342 return handle;
1345 /* Now we have to allocate a new block, but not if (fixed block or locked
1346 block) and no LMEM_MOVEABLE */
1348 if (!(flags & LMEM_MOVEABLE))
1350 if (HANDLE_FIXED(handle))
1352 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1353 return 0;
1355 else
1357 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1359 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1360 return 0;
1365 hmem = LOCAL_GetBlock( ds, size, flags );
1366 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1367 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1368 { /* a compaction, which might in turn have */
1369 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1370 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1372 if (!hmem)
1374 /* Remove the block from the heap and try again */
1375 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1376 if (!buffer) return 0;
1377 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1378 LOCAL_FreeArena( ds, arena );
1379 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1381 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1383 ERR("Can't restore saved block\n" );
1384 HeapFree( GetProcessHeap(), 0, buffer );
1385 return 0;
1387 size = oldsize;
1389 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1390 memcpy( ptr + hmem, buffer, oldsize );
1391 HeapFree( GetProcessHeap(), 0, buffer );
1393 else
1395 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1396 LOCAL_FreeArena( ds, arena );
1398 if (HANDLE_MOVEABLE( handle ))
1400 TRACE("fixing handle\n");
1401 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1402 pEntry->addr = hmem + MOVEABLE_PREFIX;
1403 /* Back ptr should still be correct */
1404 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1405 ERR("back ptr is invalid.\n");
1406 hmem = handle;
1408 if (size == oldsize) hmem = 0; /* Realloc failed */
1409 TRACE("returning %04x\n", hmem );
1410 return hmem;
1414 /***********************************************************************
1415 * LOCAL_InternalLock
1417 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1419 HLOCAL16 old_handle = handle;
1421 if (HANDLE_MOVEABLE(handle))
1423 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1424 if (pEntry->flags == (LMEM_DISCARDED >> 8)) return 0;
1425 if (pEntry->lock < 0xfe) pEntry->lock++;
1426 handle = pEntry->addr;
1428 TRACE("%04x returning %04x\n", old_handle, handle );
1429 return handle;
1433 /***********************************************************************
1434 * LocalUnlock (KERNEL.9)
1436 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1438 HANDLE16 ds = CURRENT_DS;
1439 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1441 TRACE("%04x\n", handle );
1442 if (HANDLE_MOVEABLE(handle))
1444 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1445 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1446 /* For moveable block, return the new lock count */
1447 /* (see _Windows_Internals_ p. 197) */
1448 return --pEntry->lock;
1450 else return FALSE;
1454 /***********************************************************************
1455 * LocalSize (KERNEL.10)
1457 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1459 HANDLE16 ds = CURRENT_DS;
1460 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1461 LOCALARENA *pArena;
1463 TRACE("%04x ds=%04x\n", handle, ds );
1465 if (!handle) return 0;
1466 if (HANDLE_MOVEABLE( handle ))
1468 handle = *(WORD *)(ptr + handle);
1469 if (!handle) return 0;
1470 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1472 else
1473 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1475 return pArena->next - handle;
1479 /***********************************************************************
1480 * LocalFlags (KERNEL.12)
1482 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1484 HANDLE16 ds = CURRENT_DS;
1485 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1487 if (HANDLE_MOVEABLE(handle))
1489 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1490 TRACE("(%04x,%04x): returning %04x\n",
1491 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1492 return pEntry->lock | (pEntry->flags << 8);
1494 else
1496 TRACE("(%04x,%04x): returning 0\n",
1497 ds, handle );
1498 return 0;
1503 /***********************************************************************
1504 * LocalHeapSize (KERNEL.162)
1506 WORD WINAPI LocalHeapSize16(void)
1508 HANDLE16 ds = CURRENT_DS;
1509 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1510 return pInfo ? pInfo->last - pInfo->first : 0;
1514 /***********************************************************************
1515 * LocalCountFree (KERNEL.161)
1517 WORD WINAPI LocalCountFree16(void)
1519 HANDLE16 ds = CURRENT_DS;
1520 WORD arena, total;
1521 LOCALARENA *pArena;
1522 LOCALHEAPINFO *pInfo;
1523 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1525 if (!(pInfo = LOCAL_GetHeap( ds )))
1527 ERR("(%04x): Local heap not found\n", ds );
1528 LOCAL_PrintHeap( ds );
1529 return 0;
1532 total = 0;
1533 arena = pInfo->first;
1534 pArena = ARENA_PTR( ptr, arena );
1535 for (;;)
1537 arena = pArena->free_next;
1538 pArena = ARENA_PTR( ptr, arena );
1539 if (arena == pArena->free_next) break;
1540 total += pArena->size;
1542 TRACE("(%04x): returning %d\n", ds, total);
1543 return total;
1547 /***********************************************************************
1548 * LocalHandle (KERNEL.11)
1550 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1552 HANDLE16 ds = CURRENT_DS;
1553 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1554 LOCALHEAPINFO *pInfo;
1555 WORD table;
1557 if (!(pInfo = LOCAL_GetHeap( ds )))
1559 ERR("(%04x): Local heap not found\n", ds );
1560 LOCAL_PrintHeap( ds );
1561 return 0;
1564 /* Find the address in the entry tables */
1566 table = pInfo->htable;
1567 while (table)
1569 WORD count = *(WORD *)(ptr + table);
1570 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1571 for (; count > 0; count--, pEntry++)
1572 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1573 table = *(WORD *)pEntry;
1576 return (HLOCAL16)addr; /* Fixed block handle is addr */
1582 /***********************************************************************
1583 * LocalLock (KERNEL.8)
1585 * Note: only the offset part of the pointer is returned by the relay code.
1587 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1589 WORD ds = CURRENT_DS;
1590 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1591 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1595 /***********************************************************************
1596 * LocalCompact (KERNEL.13)
1598 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1600 TRACE("%04x\n", minfree );
1601 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1605 /***********************************************************************
1606 * LocalNotify (KERNEL.14)
1608 * Installs a callback function that is called for local memory events
1609 * Callback function prototype is
1610 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1611 * wMsg:
1612 * - LN_OUTOFMEM
1613 * NotifyFunc seems to be responsible for allocating some memory,
1614 * returns TRUE for success.
1615 * wArg = number of bytes needed additionally
1616 * - LN_MOVE
1617 * hMem = handle; wArg = old mem location
1618 * - LN_DISCARD
1619 * NotifyFunc seems to be strongly encouraged to return TRUE,
1620 * otherwise LogError() gets called.
1621 * hMem = handle; wArg = flags
1623 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1625 LOCALHEAPINFO *pInfo;
1626 FARPROC16 oldNotify;
1627 HANDLE16 ds = CURRENT_DS;
1629 if (!(pInfo = LOCAL_GetHeap( ds )))
1631 ERR("(%04x): Local heap not found\n", ds );
1632 LOCAL_PrintHeap( ds );
1633 return 0;
1635 TRACE("(%04x): %p\n", ds, func );
1636 FIXME("Half implemented\n");
1637 oldNotify = pInfo->notify;
1638 pInfo->notify = func;
1639 return oldNotify;
1643 /***********************************************************************
1644 * LocalShrink (KERNEL.121)
1646 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1648 TRACE("%04x %04x\n", handle, newsize );
1649 return 0;
1653 /***********************************************************************
1654 * GetHeapSpaces (KERNEL.138)
1656 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1658 NE_MODULE *pModule;
1659 WORD oldDS = CURRENT_DS;
1660 DWORD spaces;
1662 if (!(pModule = NE_GetPtr( module ))) return 0;
1663 CURRENT_DS = GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1)->hSeg);
1664 spaces = MAKELONG( LocalCountFree16(), LocalHeapSize16() );
1665 CURRENT_DS = oldDS;
1666 return spaces;
1670 /***********************************************************************
1671 * LocalHandleDelta (KERNEL.310)
1673 WORD WINAPI LocalHandleDelta16( WORD delta )
1675 LOCALHEAPINFO *pInfo;
1677 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1679 ERR("Local heap not found\n");
1680 LOCAL_PrintHeap( CURRENT_DS );
1681 return 0;
1683 if (delta) pInfo->hdelta = delta;
1684 TRACE("returning %04x\n", pInfo->hdelta);
1685 return pInfo->hdelta;
1689 /***********************************************************************
1690 * LocalInfo (TOOLHELP.56)
1692 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1694 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(WOWGlobalLock16(handle)));
1695 if (!pInfo) return FALSE;
1696 pLocalInfo->wcItems = pInfo->items;
1697 return TRUE;
1701 /***********************************************************************
1702 * LocalFirst (TOOLHELP.57)
1704 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1706 WORD ds = GlobalHandleToSel16( handle );
1707 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1708 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1709 if (!pInfo) return FALSE;
1711 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1712 pLocalEntry->wAddress = pLocalEntry->hHandle;
1713 pLocalEntry->wFlags = LF_FIXED;
1714 pLocalEntry->wcLock = 0;
1715 pLocalEntry->wType = LT_NORMAL;
1716 pLocalEntry->hHeap = handle;
1717 pLocalEntry->wHeapType = NORMAL_HEAP;
1718 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1719 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1720 return TRUE;
1724 /***********************************************************************
1725 * LocalNext (TOOLHELP.58)
1727 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1729 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1730 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1731 LOCALARENA *pArena;
1733 if (!LOCAL_GetHeap( ds )) return FALSE;
1734 if (!pLocalEntry->wNext) return FALSE;
1735 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1737 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1738 pLocalEntry->wAddress = pLocalEntry->hHandle;
1739 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1740 pLocalEntry->wcLock = 0;
1741 pLocalEntry->wType = LT_NORMAL;
1742 if (pArena->next != pLocalEntry->wNext) /* last one? */
1743 pLocalEntry->wNext = pArena->next;
1744 else
1745 pLocalEntry->wNext = 0;
1746 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1747 return TRUE;
1751 /***********************************************************************
1752 * 32-bit local heap functions (Win95; undocumented)
1755 /***********************************************************************
1756 * K208 (KERNEL.208)
1758 HANDLE WINAPI Local32Init16( WORD segment, DWORD tableSize,
1759 DWORD heapSize, DWORD flags )
1761 DWORD totSize, segSize = 0;
1762 LPBYTE base;
1763 LOCAL32HEADER *header;
1764 HANDLE heap;
1765 WORD *selectorTable;
1766 WORD selectorEven, selectorOdd;
1767 int i, nrBlocks;
1769 /* Determine new heap size */
1771 if ( segment )
1773 if ( (segSize = GetSelectorLimit16( segment )) == 0 )
1774 return 0;
1775 else
1776 segSize++;
1779 if ( heapSize == (DWORD)-1 )
1780 heapSize = 1024*1024; /* FIXME */
1782 heapSize = (heapSize + 0xffff) & 0xffff0000;
1783 segSize = (segSize + 0x0fff) & 0xfffff000;
1784 totSize = segSize + HTABLE_SIZE + heapSize;
1787 /* Allocate memory and initialize heap */
1789 if ( !(base = VirtualAlloc( NULL, totSize, MEM_RESERVE, PAGE_READWRITE )) )
1790 return 0;
1792 if ( !VirtualAlloc( base, segSize + HTABLE_PAGESIZE,
1793 MEM_COMMIT, PAGE_READWRITE ) )
1795 VirtualFree( base, 0, MEM_RELEASE );
1796 return 0;
1799 if (!(heap = RtlCreateHeap( 0, base + segSize + HTABLE_SIZE, heapSize, 0x10000, NULL, NULL )))
1801 VirtualFree( base, 0, MEM_RELEASE );
1802 return 0;
1806 /* Set up header and handle table */
1808 header = (LOCAL32HEADER *)(base + segSize);
1809 header->base = base;
1810 header->limit = HTABLE_PAGESIZE-1;
1811 header->flags = 0;
1812 header->magic = LOCAL32_MAGIC;
1813 header->heap = heap;
1815 header->freeListFirst[0] = sizeof(LOCAL32HEADER);
1816 header->freeListLast[0] = HTABLE_PAGESIZE - 4;
1817 header->freeListSize[0] = (HTABLE_PAGESIZE - sizeof(LOCAL32HEADER)) / 4;
1819 for (i = header->freeListFirst[0]; i < header->freeListLast[0]; i += 4)
1820 *(DWORD *)((LPBYTE)header + i) = i+4;
1822 header->freeListFirst[1] = 0xffff;
1825 /* Set up selector table */
1827 nrBlocks = (totSize + 0x7fff) >> 15;
1828 selectorTable = HeapAlloc( header->heap, 0, nrBlocks * 2 );
1829 selectorEven = SELECTOR_AllocBlock( base, totSize, WINE_LDT_FLAGS_DATA );
1830 selectorOdd = SELECTOR_AllocBlock( base + 0x8000, totSize - 0x8000, WINE_LDT_FLAGS_DATA );
1831 if ( !selectorTable || !selectorEven || !selectorOdd )
1833 HeapFree( header->heap, 0, selectorTable );
1834 if ( selectorEven ) SELECTOR_FreeBlock( selectorEven );
1835 if ( selectorOdd ) SELECTOR_FreeBlock( selectorOdd );
1836 HeapDestroy( header->heap );
1837 VirtualFree( base, 0, MEM_RELEASE );
1838 return 0;
1841 header->selectorTableOffset = (LPBYTE)selectorTable - header->base;
1842 header->selectorTableSize = nrBlocks * 4; /* ??? Win95 does it this way! */
1843 header->selectorDelta = selectorEven - selectorOdd;
1844 header->segment = segment? segment : selectorEven;
1846 for (i = 0; i < nrBlocks; i++)
1847 selectorTable[i] = (i & 1)? selectorOdd + ((i >> 1) << __AHSHIFT)
1848 : selectorEven + ((i >> 1) << __AHSHIFT);
1850 /* Move old segment */
1852 if ( segment )
1854 /* FIXME: This is somewhat ugly and relies on implementation
1855 details about 16-bit global memory handles ... */
1857 LPBYTE oldBase = (LPBYTE)GetSelectorBase( segment );
1858 memcpy( base, oldBase, segSize );
1859 GLOBAL_MoveBlock( segment, base, totSize );
1860 HeapFree( GetProcessHeap(), 0, oldBase );
1863 return (HANDLE)header;
1866 /***********************************************************************
1867 * Local32_SearchHandle
1869 static LPDWORD Local32_SearchHandle( LOCAL32HEADER *header, DWORD addr )
1871 LPDWORD handle;
1873 for ( handle = (LPDWORD)((LPBYTE)header + sizeof(LOCAL32HEADER));
1874 handle < (LPDWORD)((LPBYTE)header + header->limit);
1875 handle++)
1877 if (*handle == addr)
1878 return handle;
1881 return NULL;
1884 /***********************************************************************
1885 * Local32_ToHandle
1887 static VOID Local32_ToHandle( LOCAL32HEADER *header, INT16 type,
1888 DWORD addr, LPDWORD *handle, LPBYTE *ptr )
1890 *handle = NULL;
1891 *ptr = NULL;
1893 switch (type)
1895 case -2: /* 16:16 pointer, no handles */
1896 *ptr = MapSL( addr );
1897 *handle = (LPDWORD)*ptr;
1898 break;
1900 case -1: /* 32-bit offset, no handles */
1901 *ptr = header->base + addr;
1902 *handle = (LPDWORD)*ptr;
1903 break;
1905 case 0: /* handle */
1906 if ( addr >= sizeof(LOCAL32HEADER)
1907 && addr < header->limit && !(addr & 3)
1908 && *(LPDWORD)((LPBYTE)header + addr) >= HTABLE_SIZE )
1910 *handle = (LPDWORD)((LPBYTE)header + addr);
1911 *ptr = header->base + **handle;
1913 break;
1915 case 1: /* 16:16 pointer */
1916 *ptr = MapSL( addr );
1917 *handle = Local32_SearchHandle( header, *ptr - header->base );
1918 break;
1920 case 2: /* 32-bit offset */
1921 *ptr = header->base + addr;
1922 *handle = Local32_SearchHandle( header, *ptr - header->base );
1923 break;
1927 /***********************************************************************
1928 * Local32_FromHandle
1930 static VOID Local32_FromHandle( LOCAL32HEADER *header, INT16 type,
1931 DWORD *addr, LPDWORD handle, LPBYTE ptr )
1933 *addr = 0;
1934 switch (type)
1936 case -2: /* 16:16 pointer */
1937 case 1:
1939 WORD *selTable = (LPWORD)(header->base + header->selectorTableOffset);
1940 DWORD offset = ptr - header->base;
1941 *addr = MAKELONG( offset & 0x7fff, selTable[offset >> 15] );
1943 break;
1945 case -1: /* 32-bit offset */
1946 case 2:
1947 *addr = ptr - header->base;
1948 break;
1950 case 0: /* handle */
1951 *addr = (LPBYTE)handle - (LPBYTE)header;
1952 break;
1956 /***********************************************************************
1957 * K209 (KERNEL.209)
1959 DWORD WINAPI Local32Alloc16( HANDLE heap, DWORD size, INT16 type, DWORD flags )
1961 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1962 LPDWORD handle;
1963 LPBYTE ptr;
1964 DWORD addr;
1966 /* Allocate memory */
1967 ptr = HeapAlloc( header->heap,
1968 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0, size );
1969 if (!ptr) return 0;
1972 /* Allocate handle if requested */
1973 if (type >= 0)
1975 int page, i;
1977 /* Find first page of handle table with free slots */
1978 for (page = 0; page < HTABLE_NPAGES; page++)
1979 if (header->freeListFirst[page] != 0)
1980 break;
1981 if (page == HTABLE_NPAGES)
1983 WARN("Out of handles!\n" );
1984 HeapFree( header->heap, 0, ptr );
1985 return 0;
1988 /* If virgin page, initialize it */
1989 if (header->freeListFirst[page] == 0xffff)
1991 if ( !VirtualAlloc( (LPBYTE)header + (page << 12),
1992 0x1000, MEM_COMMIT, PAGE_READWRITE ) )
1994 WARN("Cannot grow handle table!\n" );
1995 HeapFree( header->heap, 0, ptr );
1996 return 0;
1999 header->limit += HTABLE_PAGESIZE;
2001 header->freeListFirst[page] = 0;
2002 header->freeListLast[page] = HTABLE_PAGESIZE - 4;
2003 header->freeListSize[page] = HTABLE_PAGESIZE / 4;
2005 for (i = 0; i < HTABLE_PAGESIZE; i += 4)
2006 *(DWORD *)((LPBYTE)header + i) = i+4;
2008 if (page < HTABLE_NPAGES-1)
2009 header->freeListFirst[page+1] = 0xffff;
2012 /* Allocate handle slot from page */
2013 handle = (LPDWORD)((LPBYTE)header + header->freeListFirst[page]);
2014 if (--header->freeListSize[page] == 0)
2015 header->freeListFirst[page] = header->freeListLast[page] = 0;
2016 else
2017 header->freeListFirst[page] = *handle;
2019 /* Store 32-bit offset in handle slot */
2020 *handle = ptr - header->base;
2022 else
2024 handle = (LPDWORD)ptr;
2025 header->flags |= 1;
2029 /* Convert handle to requested output type */
2030 Local32_FromHandle( header, type, &addr, handle, ptr );
2031 return addr;
2034 /***********************************************************************
2035 * K210 (KERNEL.210)
2037 DWORD WINAPI Local32ReAlloc16( HANDLE heap, DWORD addr, INT16 type,
2038 DWORD size, DWORD flags )
2040 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2041 LPDWORD handle;
2042 LPBYTE ptr;
2044 if (!addr)
2045 return Local32Alloc16( heap, size, type, flags );
2047 /* Retrieve handle and pointer */
2048 Local32_ToHandle( header, type, addr, &handle, &ptr );
2049 if (!handle) return FALSE;
2051 /* Reallocate memory block */
2052 ptr = HeapReAlloc( header->heap,
2053 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0,
2054 ptr, size );
2055 if (!ptr) return 0;
2057 /* Modify handle */
2058 if (type >= 0)
2059 *handle = ptr - header->base;
2060 else
2061 handle = (LPDWORD)ptr;
2063 /* Convert handle to requested output type */
2064 Local32_FromHandle( header, type, &addr, handle, ptr );
2065 return addr;
2068 /***********************************************************************
2069 * K211 (KERNEL.211)
2071 BOOL WINAPI Local32Free16( HANDLE heap, DWORD addr, INT16 type )
2073 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2074 LPDWORD handle;
2075 LPBYTE ptr;
2077 /* Retrieve handle and pointer */
2078 Local32_ToHandle( header, type, addr, &handle, &ptr );
2079 if (!handle) return FALSE;
2081 /* Free handle if necessary */
2082 if (type >= 0)
2084 int offset = (LPBYTE)handle - (LPBYTE)header;
2085 int page = offset >> 12;
2087 /* Return handle slot to page free list */
2088 if (header->freeListSize[page]++ == 0)
2089 header->freeListFirst[page] = header->freeListLast[page] = offset;
2090 else
2091 *(LPDWORD)((LPBYTE)header + header->freeListLast[page]) = offset,
2092 header->freeListLast[page] = offset;
2094 *handle = 0;
2096 /* Shrink handle table when possible */
2097 while (page > 0 && header->freeListSize[page] == HTABLE_PAGESIZE / 4)
2099 if ( VirtualFree( (LPBYTE)header +
2100 (header->limit & ~(HTABLE_PAGESIZE-1)),
2101 HTABLE_PAGESIZE, MEM_DECOMMIT ) )
2102 break;
2104 header->limit -= HTABLE_PAGESIZE;
2105 header->freeListFirst[page] = 0xffff;
2106 page--;
2110 /* Free memory */
2111 return HeapFree( header->heap, 0, ptr );
2114 /***********************************************************************
2115 * K213 (KERNEL.213)
2117 DWORD WINAPI Local32Translate16( HANDLE heap, DWORD addr, INT16 type1, INT16 type2 )
2119 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2120 LPDWORD handle;
2121 LPBYTE ptr;
2123 Local32_ToHandle( header, type1, addr, &handle, &ptr );
2124 if (!handle) return 0;
2126 Local32_FromHandle( header, type2, &addr, handle, ptr );
2127 return addr;
2130 /***********************************************************************
2131 * K214 (KERNEL.214)
2133 DWORD WINAPI Local32Size16( HANDLE heap, DWORD addr, INT16 type )
2135 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2136 LPDWORD handle;
2137 LPBYTE ptr;
2139 Local32_ToHandle( header, type, addr, &handle, &ptr );
2140 if (!handle) return 0;
2142 return HeapSize( header->heap, 0, ptr );
2145 /***********************************************************************
2146 * K215 (KERNEL.215)
2148 BOOL WINAPI Local32ValidHandle16( HANDLE heap, WORD addr )
2150 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2151 LPDWORD handle;
2152 LPBYTE ptr;
2154 Local32_ToHandle( header, 0, addr, &handle, &ptr );
2155 return handle != NULL;
2158 /***********************************************************************
2159 * K229 (KERNEL.229)
2161 WORD WINAPI Local32GetSegment16( HANDLE heap )
2163 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2164 return header->segment;
2167 /***********************************************************************
2168 * Local32_GetHeap
2170 static LOCAL32HEADER *Local32_GetHeap( HGLOBAL16 handle )
2172 WORD selector = GlobalHandleToSel16( handle );
2173 DWORD base = GetSelectorBase( selector );
2174 DWORD limit = GetSelectorLimit16( selector );
2176 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2177 it this way ... */
2179 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2180 return (LOCAL32HEADER *)base;
2182 base += 0x10000;
2183 limit -= 0x10000;
2185 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2186 return (LOCAL32HEADER *)base;
2188 return NULL;
2191 /***********************************************************************
2192 * Local32Info (KERNEL.444)
2193 * Local32Info (TOOLHELP.84)
2195 BOOL16 WINAPI Local32Info16( LOCAL32INFO *pLocal32Info, HGLOBAL16 handle )
2197 PROCESS_HEAP_ENTRY entry;
2198 int i;
2200 LOCAL32HEADER *header = Local32_GetHeap( handle );
2201 if ( !header ) return FALSE;
2203 if ( !pLocal32Info || pLocal32Info->dwSize < sizeof(LOCAL32INFO) )
2204 return FALSE;
2206 pLocal32Info->dwMemReserved = 0;
2207 pLocal32Info->dwMemCommitted = 0;
2208 pLocal32Info->dwTotalFree = 0;
2209 pLocal32Info->dwLargestFreeBlock = 0;
2211 while (HeapWalk( header->heap, &entry ))
2213 if (entry.wFlags & PROCESS_HEAP_REGION)
2215 pLocal32Info->dwMemReserved += entry.u.Region.dwCommittedSize
2216 + entry.u.Region.dwUnCommittedSize;
2217 pLocal32Info->dwMemCommitted = entry.u.Region.dwCommittedSize;
2219 else if (!(entry.wFlags & PROCESS_HEAP_ENTRY_BUSY))
2221 DWORD size = entry.cbData + entry.cbOverhead;
2222 pLocal32Info->dwTotalFree += size;
2223 if (size > pLocal32Info->dwLargestFreeBlock) pLocal32Info->dwLargestFreeBlock = size;
2227 pLocal32Info->dwcFreeHandles = 0;
2228 for ( i = 0; i < HTABLE_NPAGES; i++ )
2230 if ( header->freeListFirst[i] == 0xffff ) break;
2231 pLocal32Info->dwcFreeHandles += header->freeListSize[i];
2233 pLocal32Info->dwcFreeHandles += (HTABLE_NPAGES - i) * HTABLE_PAGESIZE/4;
2235 return TRUE;
2238 /***********************************************************************
2239 * Local32First (KERNEL.445)
2240 * Local32First (TOOLHELP.85)
2242 BOOL16 WINAPI Local32First16( LOCAL32ENTRY *pLocal32Entry, HGLOBAL16 handle )
2244 FIXME("(%p, %04X): stub!\n", pLocal32Entry, handle );
2245 return FALSE;
2248 /***********************************************************************
2249 * Local32Next (KERNEL.446)
2250 * Local32Next (TOOLHELP.86)
2252 BOOL16 WINAPI Local32Next16( LOCAL32ENTRY *pLocal32Entry )
2254 FIXME("(%p): stub!\n", pLocal32Entry );
2255 return FALSE;