Swedish translation of winefile resources by David Nordenberg,
[wine/testsucceed.git] / dlls / kernel / local16.c
blob5d513fe79faf7bdaa878d7c82e2e16c1d10e3ccc
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "local.h"
39 #include "module.h"
40 #include "stackframe.h"
41 #include "toolhelp.h"
42 #include "kernel_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(local);
47 typedef struct
49 /* Arena header */
50 WORD prev; /* Previous arena | arena type */
51 WORD next; /* Next arena */
52 /* Start of the memory block or free-list info */
53 WORD size; /* Size of the free block */
54 WORD free_prev; /* Previous free block */
55 WORD free_next; /* Next free block */
56 } LOCALARENA;
58 #define ARENA_HEADER_SIZE 4
59 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
61 /* Arena types (stored in 'prev' field of the arena) */
62 #define LOCAL_ARENA_FREE 0
63 #define LOCAL_ARENA_FIXED 1
65 /* LocalNotify() msgs */
67 #define LN_OUTOFMEM 0
68 #define LN_MOVE 1
69 #define LN_DISCARD 2
71 /* Layout of a handle entry table
73 * WORD count of entries
74 * LOCALHANDLEENTRY[count] entries
75 * WORD near ptr to next table
77 typedef struct
79 WORD addr; /* Address of the MOVEABLE block */
80 BYTE flags; /* Flags for this block */
81 BYTE lock; /* Lock count */
82 } LOCALHANDLEENTRY;
85 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
86 * in case something actually relies on this.
87 * Note that if the architecture does not allow unaligned accesses, we make
88 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
90 * An unused handle has lock = flags = 0xff. In windows addr is that of next
91 * free handle, at the moment in wine we set it to 0.
93 * A discarded block's handle has lock = addr = 0 and flags = 0x40
94 * (LMEM_DISCARDED >> 8)
97 #ifdef ALLOW_UNALIGNED_ACCESS
98 # define MOVEABLE_PREFIX sizeof(HLOCAL16)
99 #else
100 # define MOVEABLE_PREFIX sizeof(int)
101 #endif
104 #include "pshpack1.h"
106 typedef struct
108 WORD check; /* 00 Heap checking flag */
109 WORD freeze; /* 02 Heap frozen flag */
110 WORD items; /* 04 Count of items on the heap */
111 WORD first; /* 06 First item of the heap */
112 WORD pad1; /* 08 Always 0 */
113 WORD last; /* 0a Last item of the heap */
114 WORD pad2; /* 0c Always 0 */
115 BYTE ncompact; /* 0e Compactions counter */
116 BYTE dislevel; /* 0f Discard level */
117 DWORD distotal; /* 10 Total bytes discarded */
118 WORD htable; /* 14 Pointer to handle table */
119 WORD hfree; /* 16 Pointer to free handle table */
120 WORD hdelta; /* 18 Delta to expand the handle table */
121 WORD expand; /* 1a Pointer to expand function (unused) */
122 WORD pstat; /* 1c Pointer to status structure (unused) */
123 FARPROC16 notify; /* 1e Pointer to LocalNotify() function */
124 WORD lock; /* 22 Lock count for the heap */
125 WORD extra; /* 24 Extra bytes to allocate when expanding */
126 WORD minsize; /* 26 Minimum size of the heap */
127 WORD magic; /* 28 Magic number */
128 } LOCALHEAPINFO;
130 #include "poppack.h"
132 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
134 /* All local heap allocations are aligned on 4-byte boundaries */
135 #define LALIGN(word) (((word) + 3) & ~3)
137 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
138 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
139 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
140 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
142 /* determine whether the handle belongs to a fixed or a moveable block */
143 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
144 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
147 /* 32-bit heap definitions */
149 #define HTABLE_SIZE 0x10000
150 #define HTABLE_PAGESIZE 0x1000
151 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
153 #include "pshpack1.h"
154 typedef struct _LOCAL32HEADER
156 WORD freeListFirst[HTABLE_NPAGES];
157 WORD freeListSize[HTABLE_NPAGES];
158 WORD freeListLast[HTABLE_NPAGES];
160 DWORD selectorTableOffset;
161 WORD selectorTableSize;
162 WORD selectorDelta;
164 DWORD segment;
165 LPBYTE base;
167 DWORD limit;
168 DWORD flags;
170 DWORD magic;
171 HANDLE heap;
173 } LOCAL32HEADER;
174 #include "poppack.h"
176 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
179 static inline BOOL16 call_notify_func( FARPROC16 proc, WORD msg, HLOCAL16 handle, WORD arg )
181 DWORD ret;
182 WORD args[3];
184 if (!proc) return FALSE;
185 args[2] = msg;
186 args[1] = handle;
187 args[0] = arg;
188 WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
189 return LOWORD(ret);
193 /***********************************************************************
194 * LOCAL_GetHeap
196 * Return a pointer to the local heap, making sure it exists.
198 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
200 LOCALHEAPINFO *pInfo;
201 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
202 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
203 if (!ptr || !ptr->heap) return NULL;
204 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
206 WARN("Bad pointer\n");
207 return NULL;
209 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
210 if (pInfo->magic != LOCAL_HEAP_MAGIC)
212 WARN("Bad magic\n");
213 return NULL;
215 return pInfo;
219 /***********************************************************************
220 * LOCAL_MakeBlockFree
222 * Make a block free, inserting it in the free-list.
223 * 'block' is the handle of the block arena; 'baseptr' points to
224 * the beginning of the data segment containing the heap.
226 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
228 LOCALARENA *pArena, *pNext;
229 WORD next;
231 /* Mark the block as free */
233 pArena = ARENA_PTR( baseptr, block );
234 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
235 pArena->size = pArena->next - block;
237 /* Find the next free block (last block is always free) */
239 next = pArena->next;
240 for (;;)
242 pNext = ARENA_PTR( baseptr, next );
243 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
244 next = pNext->next;
247 TRACE("%04x, next %04x\n", block, next );
248 /* Insert the free block in the free-list */
250 pArena->free_prev = pNext->free_prev;
251 pArena->free_next = next;
252 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
253 pNext->free_prev = block;
257 /***********************************************************************
258 * LOCAL_RemoveFreeBlock
260 * Remove a block from the free-list.
261 * 'block' is the handle of the block arena; 'baseptr' points to
262 * the beginning of the data segment containing the heap.
264 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
266 /* Mark the block as fixed */
268 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
269 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
271 /* Remove it from the list */
273 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
274 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
278 /***********************************************************************
279 * LOCAL_AddBlock
281 * Insert a new block in the heap.
282 * 'new' is the handle of the new block arena; 'baseptr' points to
283 * the beginning of the data segment containing the heap; 'prev' is
284 * the block before the new one.
286 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
288 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
289 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
291 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
292 pNew->next = pPrev->next;
293 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
294 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
295 pPrev->next = new;
299 /***********************************************************************
300 * LOCAL_RemoveBlock
302 * Remove a block from the heap.
303 * 'block' is the handle of the block arena; 'baseptr' points to
304 * the beginning of the data segment containing the heap.
306 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
308 LOCALARENA *pArena, *pTmp;
310 /* Remove the block from the free-list */
312 TRACE("\n");
313 pArena = ARENA_PTR( baseptr, block );
314 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
315 LOCAL_RemoveFreeBlock( baseptr, block );
317 /* If the previous block is free, expand its size */
319 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
320 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
321 pTmp->size += pArena->next - block;
323 /* Remove the block from the linked list */
325 pTmp->next = pArena->next;
326 pTmp = ARENA_PTR( baseptr, pArena->next );
327 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
331 /***********************************************************************
332 * LOCAL_PrintHeap
334 static void LOCAL_PrintHeap( HANDLE16 ds )
336 char *ptr;
337 LOCALHEAPINFO *pInfo;
338 WORD arena;
340 /* FIXME - the test should be done when calling the function!
341 plus is not clear that we should print this info
342 only when TRACE_ON is on! */
343 if(!TRACE_ON(local)) return;
345 ptr = MapSL( MAKESEGPTR( ds, 0 ));
346 pInfo = LOCAL_GetHeap( ds );
348 if (!pInfo)
350 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
351 return;
353 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
354 ds, pInfo->first, pInfo->last, pInfo->items );
356 arena = pInfo->first;
357 for (;;)
359 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
360 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
361 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
362 if (arena == pInfo->first)
364 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
365 pArena->size, pArena->free_prev, pArena->free_next );
367 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
369 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
370 pArena->size, pArena->free_prev, pArena->free_next );
371 if (pArena->next == arena) break; /* last one */
372 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
374 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
375 break;
378 if (pArena->next == arena)
380 DPRINTF( "*** last block is not marked free\n" );
381 break;
383 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
385 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
386 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
387 break;
389 arena = pArena->next;
394 /***********************************************************************
395 * LocalInit (KERNEL.4)
397 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
399 char *ptr;
400 WORD heapInfoArena, freeArena, lastArena;
401 LOCALHEAPINFO *pHeapInfo;
402 LOCALARENA *pArena, *pFirstArena, *pLastArena;
403 BOOL16 ret = FALSE;
405 /* The initial layout of the heap is: */
406 /* - first arena (FIXED) */
407 /* - heap info structure (FIXED) */
408 /* - large free block (FREE) */
409 /* - last arena (FREE) */
411 TRACE("%04x %04x-%04x\n", selector, start, end);
412 if (!selector) selector = CURRENT_DS;
414 if (TRACE_ON(local))
416 /* If TRACE_ON(heap) is set, the global heap blocks are */
417 /* cleared before use, so we can test for double initialization. */
418 if (LOCAL_GetHeap(selector))
420 ERR("Heap %04x initialized twice.\n", selector);
421 LOCAL_PrintHeap(selector);
425 if (start == 0)
427 /* start == 0 means: put the local heap at the end of the segment */
429 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
430 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
431 if ( end > 0xfffe ) end = 0xfffe;
432 start -= end;
433 end += start;
435 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
437 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
438 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
439 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
440 + sizeof(LOCALHEAPINFO) );
441 lastArena = (end - sizeof(LOCALARENA)) & ~3;
443 /* Make sure there's enough space. */
445 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
447 /* Initialise the first arena */
449 pFirstArena = ARENA_PTR( ptr, start );
450 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
451 pFirstArena->next = heapInfoArena;
452 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
453 pFirstArena->free_prev = start; /* this one */
454 pFirstArena->free_next = freeArena;
456 /* Initialise the arena of the heap info structure */
458 pArena = ARENA_PTR( ptr, heapInfoArena );
459 pArena->prev = start | LOCAL_ARENA_FIXED;
460 pArena->next = freeArena;
462 /* Initialise the heap info structure */
464 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
465 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
466 pHeapInfo->items = 4;
467 pHeapInfo->first = start;
468 pHeapInfo->last = lastArena;
469 pHeapInfo->htable = 0;
470 pHeapInfo->hdelta = 0x20;
471 pHeapInfo->extra = 0x200;
472 pHeapInfo->minsize = lastArena - freeArena;
473 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
475 /* Initialise the large free block */
477 pArena = ARENA_PTR( ptr, freeArena );
478 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
479 pArena->next = lastArena;
480 pArena->size = lastArena - freeArena;
481 pArena->free_prev = start;
482 pArena->free_next = lastArena;
484 /* Initialise the last block */
486 pLastArena = ARENA_PTR( ptr, lastArena );
487 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
488 pLastArena->next = lastArena; /* this one */
489 pLastArena->size = LALIGN(sizeof(LOCALARENA));
490 pLastArena->free_prev = freeArena;
491 pLastArena->free_next = lastArena; /* this one */
493 /* Store the local heap address in the instance data */
495 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
496 LOCAL_PrintHeap( selector );
497 ret = TRUE;
499 done:
500 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
501 return ret;
505 /***********************************************************************
506 * LOCAL_GrowHeap
508 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
510 HANDLE16 hseg;
511 LONG oldsize;
512 LONG end;
513 LOCALHEAPINFO *pHeapInfo;
514 WORD freeArena, lastArena;
515 LOCALARENA *pArena, *pLastArena;
516 char *ptr;
518 hseg = GlobalHandle16( ds );
519 /* maybe mem allocated by Virtual*() ? */
520 if (!hseg) return FALSE;
522 oldsize = GlobalSize16( hseg );
523 /* if nothing can be gained, return */
524 if (oldsize > 0xfff0) return FALSE;
525 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
526 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
527 pHeapInfo = LOCAL_GetHeap( ds );
528 if (pHeapInfo == NULL) {
529 ERR("Heap not found\n" );
530 return FALSE;
532 end = GlobalSize16( hseg );
533 lastArena = (end - sizeof(LOCALARENA)) & ~3;
535 /* Update the HeapInfo */
536 pHeapInfo->items++;
537 freeArena = pHeapInfo->last;
538 pHeapInfo->last = lastArena;
539 pHeapInfo->minsize += end - oldsize;
541 /* grow the old last block */
542 pArena = ARENA_PTR( ptr, freeArena );
543 pArena->size = lastArena - freeArena;
544 pArena->next = lastArena;
545 pArena->free_next = lastArena;
547 /* Initialise the new last block */
549 pLastArena = ARENA_PTR( ptr, lastArena );
550 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
551 pLastArena->next = lastArena; /* this one */
552 pLastArena->size = LALIGN(sizeof(LOCALARENA));
553 pLastArena->free_prev = freeArena;
554 pLastArena->free_next = lastArena; /* this one */
556 /* If block before freeArena is also free then merge them */
557 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
559 LOCAL_RemoveBlock(ptr, freeArena);
560 pHeapInfo->items--;
563 TRACE("Heap expanded\n" );
564 LOCAL_PrintHeap( ds );
565 return TRUE;
569 /***********************************************************************
570 * LOCAL_FreeArena
572 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
574 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
575 LOCALHEAPINFO *pInfo;
576 LOCALARENA *pArena, *pPrev, *pNext;
578 TRACE("%04x ds=%04x\n", arena, ds );
579 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
581 pArena = ARENA_PTR( ptr, arena );
582 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
584 /* shouldn't happen */
585 ERR("Trying to free block %04x twice!\n",
586 arena );
587 LOCAL_PrintHeap( ds );
588 return arena;
591 /* Check if we can merge with the previous block */
593 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
594 pNext = ARENA_PTR( ptr, pArena->next );
595 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
597 arena = pArena->prev & ~3;
598 pArena = pPrev;
599 LOCAL_RemoveBlock( ptr, pPrev->next );
600 pInfo->items--;
602 else /* Make a new free block */
604 LOCAL_MakeBlockFree( ptr, arena );
607 /* Check if we can merge with the next block */
609 if ((pArena->next == pArena->free_next) &&
610 (pArena->next != pInfo->last))
612 LOCAL_RemoveBlock( ptr, pArena->next );
613 pInfo->items--;
615 return 0;
619 /***********************************************************************
620 * LOCAL_ShrinkArena
622 * Shrink an arena by creating a free block at its end if possible.
623 * 'size' includes the arena header, and must be aligned.
625 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
627 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
628 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
630 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
632 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
633 if (!pInfo) return;
634 LOCAL_AddBlock( ptr, arena, arena + size );
635 pInfo->items++;
636 LOCAL_FreeArena( ds, arena + size );
641 /***********************************************************************
642 * LOCAL_GrowArenaDownward
644 * Grow an arena downward by using the previous arena (must be free).
646 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
648 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
649 LOCALHEAPINFO *pInfo;
650 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
651 WORD prevArena = pArena->prev & ~3;
652 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
653 WORD offset, size;
654 char *p;
656 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
657 offset = pPrevArena->size;
658 size = pArena->next - arena - ARENA_HEADER_SIZE;
659 LOCAL_RemoveFreeBlock( ptr, prevArena );
660 LOCAL_RemoveBlock( ptr, arena );
661 pInfo->items--;
662 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
663 while (offset < size)
665 memcpy( p, p + offset, offset );
666 p += offset;
667 size -= offset;
669 if (size) memcpy( p, p + offset, size );
670 LOCAL_ShrinkArena( ds, prevArena, newsize );
675 /***********************************************************************
676 * LOCAL_GrowArenaUpward
678 * Grow an arena upward by using the next arena (must be free and big
679 * enough). Newsize includes the arena header and must be aligned.
681 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
683 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
684 LOCALHEAPINFO *pInfo;
685 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
686 WORD nextArena = pArena->next;
688 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
689 LOCAL_RemoveBlock( ptr, nextArena );
690 pInfo->items--;
691 LOCAL_ShrinkArena( ds, arena, newsize );
695 /***********************************************************************
696 * LOCAL_GetFreeSpace
698 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
700 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
701 LOCALHEAPINFO *pInfo;
702 LOCALARENA *pArena;
703 WORD arena;
704 WORD freespace = 0;
706 if (!(pInfo = LOCAL_GetHeap( ds )))
708 ERR("Local heap not found\n" );
709 LOCAL_PrintHeap(ds);
710 return 0;
712 arena = pInfo->first;
713 pArena = ARENA_PTR( ptr, arena );
714 while (arena != pArena->free_next)
716 arena = pArena->free_next;
717 pArena = ARENA_PTR( ptr, arena );
718 if (pArena->size >= freespace) freespace = pArena->size;
720 /* FIXME doesn't yet calculate space that would become free if everything
721 were discarded when countdiscard == 1 */
722 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
723 else freespace -= ARENA_HEADER_SIZE;
724 return freespace;
728 /***********************************************************************
729 * local_compact
731 static UINT16 local_compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
733 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
734 LOCALHEAPINFO *pInfo;
735 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
736 WORD arena, movearena, finalarena, table;
737 WORD count, movesize, size;
738 WORD freespace;
739 LOCALHANDLEENTRY *pEntry;
741 if (!(pInfo = LOCAL_GetHeap( ds )))
743 ERR("Local heap not found\n" );
744 LOCAL_PrintHeap(ds);
745 return 0;
747 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
748 ds, minfree, flags);
749 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
750 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
752 TRACE("Returning %04x.\n", freespace);
753 return freespace;
755 TRACE("Compacting heap %04x.\n", ds);
756 table = pInfo->htable;
757 while(table)
759 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
760 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
762 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
764 /* OK we can move this one if we want */
765 TRACE("handle %04x (block %04x) can be moved.\n",
766 (WORD)((char *)pEntry - ptr), pEntry->addr);
767 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
768 pMoveArena = ARENA_PTR(ptr, movearena);
769 movesize = pMoveArena->next - movearena;
770 arena = pInfo->first;
771 pArena = ARENA_PTR(ptr, arena);
772 size = 0xffff;
773 finalarena = 0;
774 /* Try to find the smallest arena that will do, */
775 /* which is below us in memory */
776 for(;;)
778 arena = pArena->free_next;
779 pArena = ARENA_PTR(ptr, arena);
780 if(arena >= movearena)
781 break;
782 if(arena == pArena->free_next)
783 break;
784 if((pArena->size >= movesize) && (pArena->size < size))
786 size = pArena->size;
787 finalarena = arena;
790 if (finalarena) /* Actually got somewhere to move */
792 TRACE("Moving it to %04x.\n", finalarena);
793 pFinalArena = ARENA_PTR(ptr, finalarena);
794 size = pFinalArena->size;
795 LOCAL_RemoveFreeBlock(ptr, finalarena);
796 LOCAL_ShrinkArena( ds, finalarena, movesize );
797 /* Copy the arena to it's new location */
798 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
799 (char *)pMoveArena + ARENA_HEADER_SIZE,
800 movesize - ARENA_HEADER_SIZE );
801 /* Free the old location */
802 LOCAL_FreeArena(ds, movearena);
803 call_notify_func(pInfo->notify, LN_MOVE,
804 (WORD)((char *)pEntry - ptr), pEntry->addr);
805 /* Update handle table entry */
806 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
808 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
809 == LOCAL_ARENA_FREE)
811 /* Previous arena is free (but < movesize) */
812 /* so we can 'slide' movearena down into it */
813 finalarena = pMoveArena->prev & ~3;
814 LOCAL_GrowArenaDownward( ds, movearena, movesize );
815 /* Update handle table entry */
816 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
820 table = *(WORD *)pEntry;
822 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
823 if(freespace >= minfree || (flags & LMEM_NODISCARD))
825 TRACE("Returning %04x.\n", freespace);
826 return freespace;
829 table = pInfo->htable;
830 while(table)
832 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
833 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
835 if(pEntry->addr && pEntry->lock == 0 &&
836 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
838 TRACE("Discarding handle %04x (block %04x).\n",
839 (char *)pEntry - ptr, pEntry->addr);
840 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
841 call_notify_func(pInfo->notify, LN_DISCARD, (char *)pEntry - ptr, pEntry->flags);
842 pEntry->addr = 0;
843 pEntry->flags = (LMEM_DISCARDED >> 8);
846 table = *(WORD *)pEntry;
848 return local_compact(ds, 0xffff, LMEM_NODISCARD);
852 /***********************************************************************
853 * LOCAL_FindFreeBlock
855 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
857 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
858 LOCALHEAPINFO *pInfo;
859 LOCALARENA *pArena;
860 WORD arena;
862 if (!(pInfo = LOCAL_GetHeap( ds )))
864 ERR("Local heap not found\n" );
865 LOCAL_PrintHeap(ds);
866 return 0;
869 arena = pInfo->first;
870 pArena = ARENA_PTR( ptr, arena );
871 for (;;) {
872 arena = pArena->free_next;
873 pArena = ARENA_PTR( ptr, arena );
874 if (arena == pArena->free_next) break;
875 if (pArena->size >= size) return arena;
877 TRACE("not enough space\n" );
878 LOCAL_PrintHeap(ds);
879 return 0;
883 /***********************************************************************
884 * get_heap_name
886 static const char *get_heap_name( WORD ds )
888 HINSTANCE16 inst = LoadLibrary16( "GDI" );
889 if (ds == GlobalHandleToSel16( inst ))
891 FreeLibrary16( inst );
892 return "GDI";
894 FreeLibrary16( inst );
895 inst = LoadLibrary16( "USER" );
896 if (ds == GlobalHandleToSel16( inst ))
898 FreeLibrary16( inst );
899 return "USER";
901 FreeLibrary16( inst );
902 return "local";
905 /***********************************************************************
906 * LOCAL_GetBlock
907 * The segment may get moved around in this function, so all callers
908 * should reset their pointer variables.
910 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
912 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
913 LOCALHEAPINFO *pInfo;
914 LOCALARENA *pArena;
915 WORD arena;
917 if (!(pInfo = LOCAL_GetHeap( ds )))
919 ERR("Local heap not found\n");
920 LOCAL_PrintHeap(ds);
921 return 0;
924 size += ARENA_HEADER_SIZE;
925 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
927 #if 0
928 notify_done:
929 #endif
930 /* Find a suitable free block */
931 arena = LOCAL_FindFreeBlock( ds, size );
932 if (arena == 0) {
933 /* no space: try to make some */
934 local_compact( ds, size, flags );
935 arena = LOCAL_FindFreeBlock( ds, size );
937 if (arena == 0) {
938 /* still no space: try to grow the segment */
939 if (!(LOCAL_GrowHeap( ds )))
941 #if 0
942 /* FIXME: doesn't work correctly yet */
943 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds - 20, size)) /* FIXME: "size" correct ? (should indicate bytes needed) */
944 goto notify_done;
945 #endif
946 ERR( "not enough space in %s heap %04x for %d bytes\n",
947 get_heap_name(ds), ds, size );
948 return 0;
950 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
951 pInfo = LOCAL_GetHeap( ds );
952 arena = LOCAL_FindFreeBlock( ds, size );
954 if (arena == 0) {
955 ERR( "not enough space in %s heap %04x for %d bytes\n",
956 get_heap_name(ds), ds, size );
957 #if 0
958 /* FIXME: "size" correct ? (should indicate bytes needed) */
959 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds, size)) goto notify_done;
960 #endif
961 return 0;
964 /* Make a block out of the free arena */
965 pArena = ARENA_PTR( ptr, arena );
966 TRACE("size = %04x, arena %04x size %04x\n",
967 size, arena, pArena->size );
968 LOCAL_RemoveFreeBlock( ptr, arena );
969 LOCAL_ShrinkArena( ds, arena, size );
971 if (flags & LMEM_ZEROINIT)
972 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
973 return arena + ARENA_HEADER_SIZE;
977 /***********************************************************************
978 * LOCAL_NewHTable
980 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
982 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
983 LOCALHEAPINFO *pInfo;
984 LOCALHANDLEENTRY *pEntry;
985 HLOCAL16 handle;
986 int i;
988 TRACE("\n" );
989 if (!(pInfo = LOCAL_GetHeap( ds )))
991 ERR("Local heap not found\n");
992 LOCAL_PrintHeap(ds);
993 return FALSE;
996 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
997 + 2 * sizeof(WORD), LMEM_FIXED )))
998 return FALSE;
999 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
1000 ERR("ptr == NULL after GetBlock.\n");
1001 if (!(pInfo = LOCAL_GetHeap( ds )))
1002 ERR("pInfo == NULL after GetBlock.\n");
1004 /* Fill the entry table */
1006 *(WORD *)(ptr + handle) = pInfo->hdelta;
1007 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
1008 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
1009 pEntry->lock = pEntry->flags = 0xff;
1010 pEntry->addr = 0;
1012 *(WORD *)pEntry = pInfo->htable;
1013 pInfo->htable = handle;
1014 return TRUE;
1018 /***********************************************************************
1019 * LOCAL_GetNewHandleEntry
1021 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
1023 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1024 LOCALHEAPINFO *pInfo;
1025 LOCALHANDLEENTRY *pEntry = NULL;
1026 WORD table;
1028 if (!(pInfo = LOCAL_GetHeap( ds )))
1030 ERR("Local heap not found\n");
1031 LOCAL_PrintHeap(ds);
1032 return 0;
1035 /* Find a free slot in existing tables */
1037 table = pInfo->htable;
1038 while (table)
1040 WORD count = *(WORD *)(ptr + table);
1041 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1042 for (; count > 0; count--, pEntry++)
1043 if (pEntry->lock == 0xff) break;
1044 if (count) break;
1045 table = *(WORD *)pEntry;
1048 if (!table) /* We need to create a new table */
1050 if (!LOCAL_NewHTable( ds )) return 0;
1051 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1052 pInfo = LOCAL_GetHeap( ds );
1053 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1056 /* Now allocate this entry */
1058 pEntry->lock = 0;
1059 pEntry->flags = 0;
1060 TRACE("(%04x): %04x\n",
1061 ds, ((char *)pEntry - ptr) );
1062 return (HLOCAL16)((char *)pEntry - ptr);
1066 /***********************************************************************
1067 * LOCAL_FreeHandleEntry
1069 * Free a handle table entry.
1071 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1073 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1074 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1075 LOCALHEAPINFO *pInfo;
1076 WORD *pTable;
1077 WORD table, count, i;
1079 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1081 /* Find the table where this handle comes from */
1083 pTable = &pInfo->htable;
1084 while (*pTable)
1086 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1087 if ((handle >= *pTable + sizeof(WORD)) &&
1088 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1089 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1091 if (!*pTable)
1093 ERR("Invalid entry %04x\n", handle);
1094 LOCAL_PrintHeap( ds );
1095 return;
1098 /* Make the entry free */
1100 pEntry->addr = 0; /* just in case */
1101 pEntry->lock = 0xff;
1102 pEntry->flags = 0xff;
1103 /* Now check if all entries in this table are free */
1105 table = *pTable;
1106 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1107 count = *(WORD *)(ptr + table);
1108 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1110 /* Remove the table from the linked list and free it */
1112 TRACE("(%04x): freeing table %04x\n",
1113 ds, table);
1114 *pTable = *(WORD *)pEntry;
1115 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1119 /***********************************************************************
1120 * LocalFree (KERNEL.7)
1122 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1124 HANDLE16 ds = CURRENT_DS;
1125 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1127 TRACE("%04x ds=%04x\n", handle, ds );
1129 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1130 if (HANDLE_FIXED( handle ))
1132 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1133 else return handle; /* couldn't free it */
1135 else
1137 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1138 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1140 TRACE("real block at %04x\n",
1141 pEntry->addr );
1142 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1143 return handle; /* couldn't free it */
1145 LOCAL_FreeHandleEntry( ds, handle );
1146 return 0; /* OK */
1151 /***********************************************************************
1152 * LocalAlloc (KERNEL.5)
1154 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1156 HANDLE16 ds = CURRENT_DS;
1157 HLOCAL16 handle = 0;
1158 char *ptr;
1160 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1162 if(size > 0 && size <= 4) size = 5;
1163 if (flags & LMEM_MOVEABLE)
1165 LOCALHANDLEENTRY *plhe;
1166 HLOCAL16 hmem;
1168 if(size)
1170 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1171 goto exit;
1173 else /* We just need to allocate a discarded handle */
1174 hmem = 0;
1175 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1177 WARN("Couldn't get handle.\n");
1178 if(hmem)
1179 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1180 goto exit;
1182 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1183 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1184 plhe->lock = 0;
1185 if(hmem)
1187 plhe->addr = hmem + MOVEABLE_PREFIX;
1188 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1189 *(HLOCAL16 *)(ptr + hmem) = handle;
1191 else
1193 plhe->addr = 0;
1194 plhe->flags = LMEM_DISCARDED >> 8;
1197 else /* FIXED */
1199 if(size) handle = LOCAL_GetBlock( ds, size, flags );
1202 exit:
1203 CURRENT_STACK16->ecx = handle; /* must be returned in cx too */
1204 return handle;
1208 /***********************************************************************
1209 * LocalReAlloc (KERNEL.6)
1211 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1213 HANDLE16 ds = CURRENT_DS;
1214 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1215 LOCALHEAPINFO *pInfo;
1216 LOCALARENA *pArena, *pNext;
1217 LOCALHANDLEENTRY *pEntry = NULL;
1218 WORD arena, oldsize;
1219 HLOCAL16 hmem, blockhandle;
1220 LONG nextarena;
1222 if (!handle) return 0;
1223 if(HANDLE_MOVEABLE(handle) &&
1224 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1225 return 0;
1227 TRACE("%04x %d %04x ds=%04x\n",
1228 handle, size, flags, ds );
1229 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1231 if (HANDLE_FIXED( handle ))
1232 blockhandle = handle;
1233 else
1235 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1236 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1238 HLOCAL16 hl;
1239 if(pEntry->addr)
1240 WARN("Dicarded block has non-zero addr.\n");
1241 TRACE("ReAllocating discarded block\n");
1242 if(size <= 4) size = 5;
1243 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1244 return 0;
1245 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1246 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1247 pEntry->addr = hl + MOVEABLE_PREFIX;
1248 pEntry->flags = 0;
1249 pEntry->lock = 0;
1250 *(HLOCAL16 *)(ptr + hl) = handle;
1251 return handle;
1253 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1255 ERR("(%04x,%04x): invalid handle\n",
1256 ds, handle );
1257 return 0;
1259 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1260 ERR("Back ptr to handle is invalid\n");
1261 return 0;
1265 if (flags & LMEM_MODIFY)
1267 if (HANDLE_MOVEABLE(handle))
1269 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1270 pEntry->flags = (flags & 0x0f00) >> 8;
1271 TRACE("Changing flags to %x.\n", pEntry->flags);
1273 return handle;
1276 if (!size)
1278 if (flags & LMEM_MOVEABLE)
1280 if (HANDLE_FIXED(handle))
1282 TRACE("Freeing fixed block.\n");
1283 return LOCAL_Free( ds, handle );
1285 else /* Moveable block */
1287 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1288 if (pEntry->lock == 0)
1290 /* discards moveable blocks */
1291 TRACE("Discarding block\n");
1292 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1293 pEntry->addr = 0;
1294 pEntry->flags = (LMEM_DISCARDED >> 8);
1295 return handle;
1298 return 0;
1300 else if(flags == 0)
1302 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1303 if (pEntry->lock == 0)
1305 /* Frees block */
1306 return LOCAL_Free( ds, handle );
1309 return 0;
1312 arena = ARENA_HEADER( blockhandle );
1313 TRACE("arena is %04x\n", arena );
1314 pArena = ARENA_PTR( ptr, arena );
1316 if(size <= 4) size = 5;
1317 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1318 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1319 nextarena = LALIGN(blockhandle + size);
1321 /* Check for size reduction */
1323 if (nextarena <= pArena->next)
1325 TRACE("size reduction, making new free block\n");
1326 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1327 TRACE("returning %04x\n", handle );
1328 return handle;
1331 /* Check if the next block is free and large enough */
1333 pNext = ARENA_PTR( ptr, pArena->next );
1334 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1335 (nextarena <= pNext->next))
1337 TRACE("size increase, making new free block\n");
1338 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1339 TRACE("returning %04x\n", handle );
1340 return handle;
1343 /* Now we have to allocate a new block, but not if (fixed block or locked
1344 block) and no LMEM_MOVEABLE */
1346 if (!(flags & LMEM_MOVEABLE))
1348 if (HANDLE_FIXED(handle))
1350 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1351 return 0;
1353 else
1355 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1357 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1358 return 0;
1363 hmem = LOCAL_GetBlock( ds, size, flags );
1364 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1365 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1366 { /* a compaction, which might in turn have */
1367 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1368 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1370 if (!hmem)
1372 /* Remove the block from the heap and try again */
1373 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1374 if (!buffer) return 0;
1375 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1376 LOCAL_FreeArena( ds, arena );
1377 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1379 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1381 ERR("Can't restore saved block\n" );
1382 HeapFree( GetProcessHeap(), 0, buffer );
1383 return 0;
1385 size = oldsize;
1387 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1388 memcpy( ptr + hmem, buffer, oldsize );
1389 HeapFree( GetProcessHeap(), 0, buffer );
1391 else
1393 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1394 LOCAL_FreeArena( ds, arena );
1396 if (HANDLE_MOVEABLE( handle ))
1398 TRACE("fixing handle\n");
1399 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1400 pEntry->addr = hmem + MOVEABLE_PREFIX;
1401 /* Back ptr should still be correct */
1402 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1403 ERR("back ptr is invalid.\n");
1404 hmem = handle;
1406 if (size == oldsize) hmem = 0; /* Realloc failed */
1407 TRACE("returning %04x\n", hmem );
1408 return hmem;
1412 /***********************************************************************
1413 * LOCAL_InternalLock
1415 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1417 HLOCAL16 old_handle = handle;
1419 if (HANDLE_MOVEABLE(handle))
1421 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1422 if (pEntry->flags == (LMEM_DISCARDED >> 8)) return 0;
1423 if (pEntry->lock < 0xfe) pEntry->lock++;
1424 handle = pEntry->addr;
1426 TRACE("%04x returning %04x\n",
1427 old_handle, handle );
1428 return handle;
1432 /***********************************************************************
1433 * LocalUnlock (KERNEL.9)
1435 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1437 HANDLE16 ds = CURRENT_DS;
1438 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1440 TRACE("%04x\n", handle );
1441 if (HANDLE_MOVEABLE(handle))
1443 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1444 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1445 /* For moveable block, return the new lock count */
1446 /* (see _Windows_Internals_ p. 197) */
1447 return --pEntry->lock;
1449 else return FALSE;
1453 /***********************************************************************
1454 * LocalSize (KERNEL.10)
1456 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1458 HANDLE16 ds = CURRENT_DS;
1459 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1460 LOCALARENA *pArena;
1462 TRACE("%04x ds=%04x\n", handle, ds );
1464 if (!handle) return 0;
1465 if (HANDLE_MOVEABLE( handle ))
1467 handle = *(WORD *)(ptr + handle);
1468 if (!handle) return 0;
1469 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1471 else
1472 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1474 return pArena->next - handle;
1478 /***********************************************************************
1479 * LocalFlags (KERNEL.12)
1481 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1483 HANDLE16 ds = CURRENT_DS;
1484 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1486 if (HANDLE_MOVEABLE(handle))
1488 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1489 TRACE("(%04x,%04x): returning %04x\n",
1490 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1491 return pEntry->lock | (pEntry->flags << 8);
1493 else
1495 TRACE("(%04x,%04x): returning 0\n",
1496 ds, handle );
1497 return 0;
1502 /***********************************************************************
1503 * LocalHeapSize (KERNEL.162)
1505 WORD WINAPI LocalHeapSize16(void)
1507 HANDLE16 ds = CURRENT_DS;
1508 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1509 return pInfo ? pInfo->last - pInfo->first : 0;
1513 /***********************************************************************
1514 * LocalCountFree (KERNEL.161)
1516 WORD WINAPI LocalCountFree16(void)
1518 HANDLE16 ds = CURRENT_DS;
1519 WORD arena, total;
1520 LOCALARENA *pArena;
1521 LOCALHEAPINFO *pInfo;
1522 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1524 if (!(pInfo = LOCAL_GetHeap( ds )))
1526 ERR("(%04x): Local heap not found\n", ds );
1527 LOCAL_PrintHeap( ds );
1528 return 0;
1531 total = 0;
1532 arena = pInfo->first;
1533 pArena = ARENA_PTR( ptr, arena );
1534 for (;;)
1536 arena = pArena->free_next;
1537 pArena = ARENA_PTR( ptr, arena );
1538 if (arena == pArena->free_next) break;
1539 total += pArena->size;
1541 TRACE("(%04x): returning %d\n", ds, total);
1542 return total;
1546 /***********************************************************************
1547 * LocalHandle (KERNEL.11)
1549 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1551 HANDLE16 ds = CURRENT_DS;
1552 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1553 LOCALHEAPINFO *pInfo;
1554 WORD table;
1556 if (!(pInfo = LOCAL_GetHeap( ds )))
1558 ERR("(%04x): Local heap not found\n", ds );
1559 LOCAL_PrintHeap( ds );
1560 return 0;
1563 /* Find the address in the entry tables */
1565 table = pInfo->htable;
1566 while (table)
1568 WORD count = *(WORD *)(ptr + table);
1569 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1570 for (; count > 0; count--, pEntry++)
1571 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1572 table = *(WORD *)pEntry;
1575 return (HLOCAL16)addr; /* Fixed block handle is addr */
1581 /***********************************************************************
1582 * LocalLock (KERNEL.8)
1584 * Note: only the offset part of the pointer is returned by the relay code.
1586 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1588 WORD ds = CURRENT_DS;
1589 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1590 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1594 /***********************************************************************
1595 * LocalCompact (KERNEL.13)
1597 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1599 TRACE("%04x\n", minfree );
1600 return local_compact( CURRENT_DS, minfree, 0 );
1604 /***********************************************************************
1605 * LocalNotify (KERNEL.14)
1607 * Installs a callback function that is called for local memory events
1608 * Callback function prototype is
1609 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1610 * wMsg:
1611 * - LN_OUTOFMEM
1612 * NotifyFunc seems to be responsible for allocating some memory,
1613 * returns TRUE for success.
1614 * wArg = number of bytes needed additionally
1615 * - LN_MOVE
1616 * hMem = handle; wArg = old mem location
1617 * - LN_DISCARD
1618 * NotifyFunc seems to be strongly encouraged to return TRUE,
1619 * otherwise LogError() gets called.
1620 * hMem = handle; wArg = flags
1622 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1624 LOCALHEAPINFO *pInfo;
1625 FARPROC16 oldNotify;
1626 HANDLE16 ds = CURRENT_DS;
1628 if (!(pInfo = LOCAL_GetHeap( ds )))
1630 ERR("(%04x): Local heap not found\n", ds );
1631 LOCAL_PrintHeap( ds );
1632 return 0;
1634 TRACE("(%04x): %08lx\n", ds, (DWORD)func );
1635 FIXME("Half implemented\n");
1636 oldNotify = pInfo->notify;
1637 pInfo->notify = func;
1638 return oldNotify;
1642 /***********************************************************************
1643 * LocalShrink (KERNEL.121)
1645 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1647 TRACE("%04x %04x\n", handle, newsize );
1648 return 0;
1652 /***********************************************************************
1653 * GetHeapSpaces (KERNEL.138)
1655 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1657 NE_MODULE *pModule;
1658 WORD ds;
1660 if (!(pModule = NE_GetPtr( module ))) return 0;
1661 ds =
1662 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1663 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1667 /***********************************************************************
1668 * LocalHandleDelta (KERNEL.310)
1670 WORD WINAPI LocalHandleDelta16( WORD delta )
1672 LOCALHEAPINFO *pInfo;
1674 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1676 ERR("Local heap not found\n");
1677 LOCAL_PrintHeap( CURRENT_DS );
1678 return 0;
1680 if (delta) pInfo->hdelta = delta;
1681 TRACE("returning %04x\n", pInfo->hdelta);
1682 return pInfo->hdelta;
1686 /***********************************************************************
1687 * LocalInfo (TOOLHELP.56)
1689 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1691 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle)));
1692 if (!pInfo) return FALSE;
1693 pLocalInfo->wcItems = pInfo->items;
1694 return TRUE;
1698 /***********************************************************************
1699 * LocalFirst (TOOLHELP.57)
1701 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1703 WORD ds = GlobalHandleToSel16( handle );
1704 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1705 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1706 if (!pInfo) return FALSE;
1708 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1709 pLocalEntry->wAddress = pLocalEntry->hHandle;
1710 pLocalEntry->wFlags = LF_FIXED;
1711 pLocalEntry->wcLock = 0;
1712 pLocalEntry->wType = LT_NORMAL;
1713 pLocalEntry->hHeap = handle;
1714 pLocalEntry->wHeapType = NORMAL_HEAP;
1715 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1716 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1717 return TRUE;
1721 /***********************************************************************
1722 * LocalNext (TOOLHELP.58)
1724 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1726 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1727 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1728 LOCALARENA *pArena;
1730 if (!LOCAL_GetHeap( ds )) return FALSE;
1731 if (!pLocalEntry->wNext) return FALSE;
1732 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1734 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1735 pLocalEntry->wAddress = pLocalEntry->hHandle;
1736 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1737 pLocalEntry->wcLock = 0;
1738 pLocalEntry->wType = LT_NORMAL;
1739 if (pArena->next != pLocalEntry->wNext) /* last one? */
1740 pLocalEntry->wNext = pArena->next;
1741 else
1742 pLocalEntry->wNext = 0;
1743 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1744 return TRUE;
1748 /***********************************************************************
1749 * 32-bit local heap functions (Win95; undocumented)
1752 /***********************************************************************
1753 * K208 (KERNEL.208)
1755 HANDLE WINAPI Local32Init16( WORD segment, DWORD tableSize,
1756 DWORD heapSize, DWORD flags )
1758 DWORD totSize, segSize = 0;
1759 LPBYTE base;
1760 LOCAL32HEADER *header;
1761 HANDLE heap;
1762 WORD *selectorTable;
1763 WORD selectorEven, selectorOdd;
1764 int i, nrBlocks;
1766 /* Determine new heap size */
1768 if ( segment )
1770 if ( (segSize = GetSelectorLimit16( segment )) == 0 )
1771 return 0;
1772 else
1773 segSize++;
1776 if ( heapSize == -1L )
1777 heapSize = 1024L*1024L; /* FIXME */
1779 heapSize = (heapSize + 0xffff) & 0xffff0000;
1780 segSize = (segSize + 0x0fff) & 0xfffff000;
1781 totSize = segSize + HTABLE_SIZE + heapSize;
1784 /* Allocate memory and initialize heap */
1786 if ( !(base = VirtualAlloc( NULL, totSize, MEM_RESERVE, PAGE_READWRITE )) )
1787 return 0;
1789 if ( !VirtualAlloc( base, segSize + HTABLE_PAGESIZE,
1790 MEM_COMMIT, PAGE_READWRITE ) )
1792 VirtualFree( base, 0, MEM_RELEASE );
1793 return 0;
1796 if (!(heap = RtlCreateHeap( 0, base + segSize + HTABLE_SIZE, heapSize, 0x10000, NULL, NULL )))
1798 VirtualFree( base, 0, MEM_RELEASE );
1799 return 0;
1803 /* Set up header and handle table */
1805 header = (LOCAL32HEADER *)(base + segSize);
1806 header->base = base;
1807 header->limit = HTABLE_PAGESIZE-1;
1808 header->flags = 0;
1809 header->magic = LOCAL32_MAGIC;
1810 header->heap = heap;
1812 header->freeListFirst[0] = sizeof(LOCAL32HEADER);
1813 header->freeListLast[0] = HTABLE_PAGESIZE - 4;
1814 header->freeListSize[0] = (HTABLE_PAGESIZE - sizeof(LOCAL32HEADER)) / 4;
1816 for (i = header->freeListFirst[0]; i < header->freeListLast[0]; i += 4)
1817 *(DWORD *)((LPBYTE)header + i) = i+4;
1819 header->freeListFirst[1] = 0xffff;
1822 /* Set up selector table */
1824 nrBlocks = (totSize + 0x7fff) >> 15;
1825 selectorTable = HeapAlloc( header->heap, 0, nrBlocks * 2 );
1826 selectorEven = SELECTOR_AllocBlock( base, totSize, WINE_LDT_FLAGS_DATA );
1827 selectorOdd = SELECTOR_AllocBlock( base + 0x8000, totSize - 0x8000, WINE_LDT_FLAGS_DATA );
1828 if ( !selectorTable || !selectorEven || !selectorOdd )
1830 HeapFree( header->heap, 0, selectorTable );
1831 if ( selectorEven ) SELECTOR_FreeBlock( selectorEven );
1832 if ( selectorOdd ) SELECTOR_FreeBlock( selectorOdd );
1833 HeapDestroy( header->heap );
1834 VirtualFree( base, 0, MEM_RELEASE );
1835 return 0;
1838 header->selectorTableOffset = (LPBYTE)selectorTable - header->base;
1839 header->selectorTableSize = nrBlocks * 4; /* ??? Win95 does it this way! */
1840 header->selectorDelta = selectorEven - selectorOdd;
1841 header->segment = segment? segment : selectorEven;
1843 for (i = 0; i < nrBlocks; i++)
1844 selectorTable[i] = (i & 1)? selectorOdd + ((i >> 1) << __AHSHIFT)
1845 : selectorEven + ((i >> 1) << __AHSHIFT);
1847 /* Move old segment */
1849 if ( segment )
1851 /* FIXME: This is somewhat ugly and relies on implementation
1852 details about 16-bit global memory handles ... */
1854 LPBYTE oldBase = (LPBYTE)GetSelectorBase( segment );
1855 memcpy( base, oldBase, segSize );
1856 GLOBAL_MoveBlock( segment, base, totSize );
1857 HeapFree( GetProcessHeap(), 0, oldBase );
1860 return (HANDLE)header;
1863 /***********************************************************************
1864 * Local32_SearchHandle
1866 static LPDWORD Local32_SearchHandle( LOCAL32HEADER *header, DWORD addr )
1868 LPDWORD handle;
1870 for ( handle = (LPDWORD)((LPBYTE)header + sizeof(LOCAL32HEADER));
1871 handle < (LPDWORD)((LPBYTE)header + header->limit);
1872 handle++)
1874 if (*handle == addr)
1875 return handle;
1878 return NULL;
1881 /***********************************************************************
1882 * Local32_ToHandle
1884 static VOID Local32_ToHandle( LOCAL32HEADER *header, INT16 type,
1885 DWORD addr, LPDWORD *handle, LPBYTE *ptr )
1887 *handle = NULL;
1888 *ptr = NULL;
1890 switch (type)
1892 case -2: /* 16:16 pointer, no handles */
1893 *ptr = MapSL( addr );
1894 *handle = (LPDWORD)*ptr;
1895 break;
1897 case -1: /* 32-bit offset, no handles */
1898 *ptr = header->base + addr;
1899 *handle = (LPDWORD)*ptr;
1900 break;
1902 case 0: /* handle */
1903 if ( addr >= sizeof(LOCAL32HEADER)
1904 && addr < header->limit && !(addr & 3)
1905 && *(LPDWORD)((LPBYTE)header + addr) >= HTABLE_SIZE )
1907 *handle = (LPDWORD)((LPBYTE)header + addr);
1908 *ptr = header->base + **handle;
1910 break;
1912 case 1: /* 16:16 pointer */
1913 *ptr = MapSL( addr );
1914 *handle = Local32_SearchHandle( header, *ptr - header->base );
1915 break;
1917 case 2: /* 32-bit offset */
1918 *ptr = header->base + addr;
1919 *handle = Local32_SearchHandle( header, *ptr - header->base );
1920 break;
1924 /***********************************************************************
1925 * Local32_FromHandle
1927 static VOID Local32_FromHandle( LOCAL32HEADER *header, INT16 type,
1928 DWORD *addr, LPDWORD handle, LPBYTE ptr )
1930 switch (type)
1932 case -2: /* 16:16 pointer */
1933 case 1:
1935 WORD *selTable = (LPWORD)(header->base + header->selectorTableOffset);
1936 DWORD offset = (LPBYTE)ptr - header->base;
1937 *addr = MAKELONG( offset & 0x7fff, selTable[offset >> 15] );
1939 break;
1941 case -1: /* 32-bit offset */
1942 case 2:
1943 *addr = ptr - header->base;
1944 break;
1946 case 0: /* handle */
1947 *addr = (LPBYTE)handle - (LPBYTE)header;
1948 break;
1952 /***********************************************************************
1953 * K209 (KERNEL.209)
1955 DWORD WINAPI Local32Alloc16( HANDLE heap, DWORD size, INT16 type, DWORD flags )
1957 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1958 LPDWORD handle;
1959 LPBYTE ptr;
1960 DWORD addr;
1962 /* Allocate memory */
1963 ptr = HeapAlloc( header->heap,
1964 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0, size );
1965 if (!ptr) return 0;
1968 /* Allocate handle if requested */
1969 if (type >= 0)
1971 int page, i;
1973 /* Find first page of handle table with free slots */
1974 for (page = 0; page < HTABLE_NPAGES; page++)
1975 if (header->freeListFirst[page] != 0)
1976 break;
1977 if (page == HTABLE_NPAGES)
1979 WARN("Out of handles!\n" );
1980 HeapFree( header->heap, 0, ptr );
1981 return 0;
1984 /* If virgin page, initialize it */
1985 if (header->freeListFirst[page] == 0xffff)
1987 if ( !VirtualAlloc( (LPBYTE)header + (page << 12),
1988 0x1000, MEM_COMMIT, PAGE_READWRITE ) )
1990 WARN("Cannot grow handle table!\n" );
1991 HeapFree( header->heap, 0, ptr );
1992 return 0;
1995 header->limit += HTABLE_PAGESIZE;
1997 header->freeListFirst[page] = 0;
1998 header->freeListLast[page] = HTABLE_PAGESIZE - 4;
1999 header->freeListSize[page] = HTABLE_PAGESIZE / 4;
2001 for (i = 0; i < HTABLE_PAGESIZE; i += 4)
2002 *(DWORD *)((LPBYTE)header + i) = i+4;
2004 if (page < HTABLE_NPAGES-1)
2005 header->freeListFirst[page+1] = 0xffff;
2008 /* Allocate handle slot from page */
2009 handle = (LPDWORD)((LPBYTE)header + header->freeListFirst[page]);
2010 if (--header->freeListSize[page] == 0)
2011 header->freeListFirst[page] = header->freeListLast[page] = 0;
2012 else
2013 header->freeListFirst[page] = *handle;
2015 /* Store 32-bit offset in handle slot */
2016 *handle = ptr - header->base;
2018 else
2020 handle = (LPDWORD)ptr;
2021 header->flags |= 1;
2025 /* Convert handle to requested output type */
2026 Local32_FromHandle( header, type, &addr, handle, ptr );
2027 return addr;
2030 /***********************************************************************
2031 * K210 (KERNEL.210)
2033 DWORD WINAPI Local32ReAlloc16( HANDLE heap, DWORD addr, INT16 type,
2034 DWORD size, DWORD flags )
2036 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2037 LPDWORD handle;
2038 LPBYTE ptr;
2040 if (!addr)
2041 return Local32Alloc16( heap, size, type, flags );
2043 /* Retrieve handle and pointer */
2044 Local32_ToHandle( header, type, addr, &handle, &ptr );
2045 if (!handle) return FALSE;
2047 /* Reallocate memory block */
2048 ptr = HeapReAlloc( header->heap,
2049 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0,
2050 ptr, size );
2051 if (!ptr) return 0;
2053 /* Modify handle */
2054 if (type >= 0)
2055 *handle = ptr - header->base;
2056 else
2057 handle = (LPDWORD)ptr;
2059 /* Convert handle to requested output type */
2060 Local32_FromHandle( header, type, &addr, handle, ptr );
2061 return addr;
2064 /***********************************************************************
2065 * K211 (KERNEL.211)
2067 BOOL WINAPI Local32Free16( HANDLE heap, DWORD addr, INT16 type )
2069 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2070 LPDWORD handle;
2071 LPBYTE ptr;
2073 /* Retrieve handle and pointer */
2074 Local32_ToHandle( header, type, addr, &handle, &ptr );
2075 if (!handle) return FALSE;
2077 /* Free handle if necessary */
2078 if (type >= 0)
2080 int offset = (LPBYTE)handle - (LPBYTE)header;
2081 int page = offset >> 12;
2083 /* Return handle slot to page free list */
2084 if (header->freeListSize[page]++ == 0)
2085 header->freeListFirst[page] = header->freeListLast[page] = offset;
2086 else
2087 *(LPDWORD)((LPBYTE)header + header->freeListLast[page]) = offset,
2088 header->freeListLast[page] = offset;
2090 *handle = 0;
2092 /* Shrink handle table when possible */
2093 while (page > 0 && header->freeListSize[page] == HTABLE_PAGESIZE / 4)
2095 if ( VirtualFree( (LPBYTE)header +
2096 (header->limit & ~(HTABLE_PAGESIZE-1)),
2097 HTABLE_PAGESIZE, MEM_DECOMMIT ) )
2098 break;
2100 header->limit -= HTABLE_PAGESIZE;
2101 header->freeListFirst[page] = 0xffff;
2102 page--;
2106 /* Free memory */
2107 return HeapFree( header->heap, 0, ptr );
2110 /***********************************************************************
2111 * K213 (KERNEL.213)
2113 DWORD WINAPI Local32Translate16( HANDLE heap, DWORD addr, INT16 type1, INT16 type2 )
2115 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2116 LPDWORD handle;
2117 LPBYTE ptr;
2119 Local32_ToHandle( header, type1, addr, &handle, &ptr );
2120 if (!handle) return 0;
2122 Local32_FromHandle( header, type2, &addr, handle, ptr );
2123 return addr;
2126 /***********************************************************************
2127 * K214 (KERNEL.214)
2129 DWORD WINAPI Local32Size16( HANDLE heap, DWORD addr, INT16 type )
2131 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2132 LPDWORD handle;
2133 LPBYTE ptr;
2135 Local32_ToHandle( header, type, addr, &handle, &ptr );
2136 if (!handle) return 0;
2138 return HeapSize( header->heap, 0, ptr );
2141 /***********************************************************************
2142 * K215 (KERNEL.215)
2144 BOOL WINAPI Local32ValidHandle16( HANDLE heap, WORD addr )
2146 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2147 LPDWORD handle;
2148 LPBYTE ptr;
2150 Local32_ToHandle( header, 0, addr, &handle, &ptr );
2151 return handle != NULL;
2154 /***********************************************************************
2155 * K229 (KERNEL.229)
2157 WORD WINAPI Local32GetSegment16( HANDLE heap )
2159 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
2160 return header->segment;
2163 /***********************************************************************
2164 * Local32_GetHeap
2166 static LOCAL32HEADER *Local32_GetHeap( HGLOBAL16 handle )
2168 WORD selector = GlobalHandleToSel16( handle );
2169 DWORD base = GetSelectorBase( selector );
2170 DWORD limit = GetSelectorLimit16( selector );
2172 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2173 it this way ... */
2175 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2176 return (LOCAL32HEADER *)base;
2178 base += 0x10000;
2179 limit -= 0x10000;
2181 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2182 return (LOCAL32HEADER *)base;
2184 return NULL;
2187 /***********************************************************************
2188 * Local32Info (KERNEL.444)
2189 * Local32Info (TOOLHELP.84)
2191 BOOL16 WINAPI Local32Info16( LOCAL32INFO *pLocal32Info, HGLOBAL16 handle )
2193 PROCESS_HEAP_ENTRY entry;
2194 int i;
2196 LOCAL32HEADER *header = Local32_GetHeap( handle );
2197 if ( !header ) return FALSE;
2199 if ( !pLocal32Info || pLocal32Info->dwSize < sizeof(LOCAL32INFO) )
2200 return FALSE;
2202 pLocal32Info->dwMemReserved = 0;
2203 pLocal32Info->dwMemCommitted = 0;
2204 pLocal32Info->dwTotalFree = 0;
2205 pLocal32Info->dwLargestFreeBlock = 0;
2207 while (HeapWalk( header->heap, &entry ))
2209 if (entry.wFlags & PROCESS_HEAP_REGION)
2211 pLocal32Info->dwMemReserved += entry.u.Region.dwCommittedSize
2212 + entry.u.Region.dwUnCommittedSize;
2213 pLocal32Info->dwMemCommitted = entry.u.Region.dwCommittedSize;
2215 else if (!(entry.wFlags & PROCESS_HEAP_ENTRY_BUSY))
2217 DWORD size = entry.cbData + entry.cbOverhead;
2218 pLocal32Info->dwTotalFree += size;
2219 if (size > pLocal32Info->dwLargestFreeBlock) pLocal32Info->dwLargestFreeBlock = size;
2223 pLocal32Info->dwcFreeHandles = 0;
2224 for ( i = 0; i < HTABLE_NPAGES; i++ )
2226 if ( header->freeListFirst[i] == 0xffff ) break;
2227 pLocal32Info->dwcFreeHandles += header->freeListSize[i];
2229 pLocal32Info->dwcFreeHandles += (HTABLE_NPAGES - i) * HTABLE_PAGESIZE/4;
2231 return TRUE;
2234 /***********************************************************************
2235 * Local32First (KERNEL.445)
2236 * Local32First (TOOLHELP.85)
2238 BOOL16 WINAPI Local32First16( LOCAL32ENTRY *pLocal32Entry, HGLOBAL16 handle )
2240 FIXME("(%p, %04X): stub!\n", pLocal32Entry, handle );
2241 return FALSE;
2244 /***********************************************************************
2245 * Local32Next (KERNEL.446)
2246 * Local32Next (TOOLHELP.86)
2248 BOOL16 WINAPI Local32Next16( LOCAL32ENTRY *pLocal32Entry )
2250 FIXME("(%p): stub!\n", pLocal32Entry );
2251 return FALSE;