Fixed error recovery during thread creation.
[wine.git] / memory / local.c
blob803674e3024eb25b8bf5825f5d2c54c363e6dda0
1 /*
2 * Local heap functions
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Huw Davies
6 */
8 /*
9 * Note:
10 * All local heap functions need the current DS as first parameter
11 * when called from the emulation library, so they take one more
12 * parameter than usual.
15 #include <stdlib.h>
16 #include <string.h>
17 #include "wine/winbase16.h"
18 #include "task.h"
19 #include "heap.h"
20 #include "instance.h"
21 #include "local.h"
22 #include "module.h"
23 #include "stackframe.h"
24 #include "toolhelp.h"
25 #include "debugtools.h"
26 #include "callback.h"
28 DEFAULT_DEBUG_CHANNEL(local);
29 DECLARE_DEBUG_CHANNEL(heap);
31 typedef struct
33 /* Arena header */
34 WORD prev; /* Previous arena | arena type */
35 WORD next; /* Next arena */
36 /* Start of the memory block or free-list info */
37 WORD size; /* Size of the free block */
38 WORD free_prev; /* Previous free block */
39 WORD free_next; /* Next free block */
40 } LOCALARENA;
42 #define ARENA_HEADER_SIZE 4
43 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
45 /* Arena types (stored in 'prev' field of the arena) */
46 #define LOCAL_ARENA_FREE 0
47 #define LOCAL_ARENA_FIXED 1
49 /* LocalNotify() msgs */
51 #define LN_OUTOFMEM 0
52 #define LN_MOVE 1
53 #define LN_DISCARD 2
55 /* Layout of a handle entry table
57 * WORD count of entries
58 * LOCALHANDLEENTRY[count] entries
59 * WORD near ptr to next table
61 typedef struct
63 WORD addr; /* Address of the MOVEABLE block */
64 BYTE flags; /* Flags for this block */
65 BYTE lock; /* Lock count */
66 } LOCALHANDLEENTRY;
69 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
70 * in case something actually relies on this.
71 * Note the ARENA_HEADER(addr) still produces the desired result ie. 4n - 4
73 * An unused handle has lock = flags = 0xff. In windows addr is that of next
74 * free handle, at the moment in wine we set it to 0.
76 * A discarded block's handle has lock = addr = 0 and flags = 0x40
77 * (LMEM_DISCARDED >> 8)
80 #include "pshpack1.h"
82 typedef struct
84 WORD check; /* 00 Heap checking flag */
85 WORD freeze; /* 02 Heap frozen flag */
86 WORD items; /* 04 Count of items on the heap */
87 WORD first; /* 06 First item of the heap */
88 WORD pad1; /* 08 Always 0 */
89 WORD last; /* 0a Last item of the heap */
90 WORD pad2; /* 0c Always 0 */
91 BYTE ncompact; /* 0e Compactions counter */
92 BYTE dislevel; /* 0f Discard level */
93 DWORD distotal; /* 10 Total bytes discarded */
94 WORD htable; /* 14 Pointer to handle table */
95 WORD hfree; /* 16 Pointer to free handle table */
96 WORD hdelta; /* 18 Delta to expand the handle table */
97 WORD expand; /* 1a Pointer to expand function (unused) */
98 WORD pstat; /* 1c Pointer to status structure (unused) */
99 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
100 WORD lock; /* 22 Lock count for the heap */
101 WORD extra; /* 24 Extra bytes to allocate when expanding */
102 WORD minsize; /* 26 Minimum size of the heap */
103 WORD magic; /* 28 Magic number */
104 } LOCALHEAPINFO;
106 #include "poppack.h"
108 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
110 WORD USER_HeapSel = 0; /* USER heap selector */
112 /* All local heap allocations are aligned on 4-byte boundaries */
113 #define LALIGN(word) (((word) + 3) & ~3)
115 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
116 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
117 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
118 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
120 /* determine whether the handle belongs to a fixed or a moveable block */
121 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
122 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
125 /* ### start build ### */
126 extern WORD CALLBACK LOCAL_CallTo16_word_www(FARPROC16,WORD,HLOCAL16,WORD);
127 /* ### stop build ### */
129 /***********************************************************************
130 * LOCAL_GetHeap
132 * Return a pointer to the local heap, making sure it exists.
134 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
136 LOCALHEAPINFO *pInfo;
137 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
138 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
139 if (!ptr || !ptr->heap) return NULL;
140 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
142 WARN("Bad pointer\n");
143 return NULL;
145 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
146 if (pInfo->magic != LOCAL_HEAP_MAGIC)
148 WARN("Bad magic\n");
149 return NULL;
151 return pInfo;
155 /***********************************************************************
156 * LOCAL_MakeBlockFree
158 * Make a block free, inserting it in the free-list.
159 * 'block' is the handle of the block arena; 'baseptr' points to
160 * the beginning of the data segment containing the heap.
162 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
164 LOCALARENA *pArena, *pNext;
165 WORD next;
167 /* Mark the block as free */
169 pArena = ARENA_PTR( baseptr, block );
170 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
171 pArena->size = pArena->next - block;
173 /* Find the next free block (last block is always free) */
175 next = pArena->next;
176 for (;;)
178 pNext = ARENA_PTR( baseptr, next );
179 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
180 next = pNext->next;
183 TRACE("%04x, next %04x\n", block, next );
184 /* Insert the free block in the free-list */
186 pArena->free_prev = pNext->free_prev;
187 pArena->free_next = next;
188 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
189 pNext->free_prev = block;
193 /***********************************************************************
194 * LOCAL_RemoveFreeBlock
196 * Remove a block from the free-list.
197 * 'block' is the handle of the block arena; 'baseptr' points to
198 * the beginning of the data segment containing the heap.
200 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
202 /* Mark the block as fixed */
204 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
205 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
207 /* Remove it from the list */
209 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
210 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
214 /***********************************************************************
215 * LOCAL_AddBlock
217 * Insert a new block in the heap.
218 * 'new' is the handle of the new block arena; 'baseptr' points to
219 * the beginning of the data segment containing the heap; 'prev' is
220 * the block before the new one.
222 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
224 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
225 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
227 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
228 pNew->next = pPrev->next;
229 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
230 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
231 pPrev->next = new;
235 /***********************************************************************
236 * LOCAL_RemoveBlock
238 * Remove a block from the heap.
239 * 'block' is the handle of the block arena; 'baseptr' points to
240 * the beginning of the data segment containing the heap.
242 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
244 LOCALARENA *pArena, *pTmp;
246 /* Remove the block from the free-list */
248 TRACE("\n");
249 pArena = ARENA_PTR( baseptr, block );
250 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
251 LOCAL_RemoveFreeBlock( baseptr, block );
253 /* If the previous block is free, expand its size */
255 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
256 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
257 pTmp->size += pArena->next - block;
259 /* Remove the block from the linked list */
261 pTmp->next = pArena->next;
262 pTmp = ARENA_PTR( baseptr, pArena->next );
263 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
267 /***********************************************************************
268 * LOCAL_PrintHeap
270 static void LOCAL_PrintHeap( HANDLE16 ds )
272 char *ptr;
273 LOCALHEAPINFO *pInfo;
274 WORD arena;
276 /* FIXME - the test should be done when calling the function!
277 plus is not clear that we should print this info
278 only when TRACE_ON is on! */
279 if(!TRACE_ON(local)) return;
281 ptr = MapSL( MAKESEGPTR( ds, 0 ));
282 pInfo = LOCAL_GetHeap( ds );
284 if (!pInfo)
286 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
287 return;
289 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
290 ds, pInfo->first, pInfo->last, pInfo->items );
292 arena = pInfo->first;
293 for (;;)
295 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
296 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
297 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
298 if (arena == pInfo->first)
300 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
301 pArena->size, pArena->free_prev, pArena->free_next );
303 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
305 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
306 pArena->size, pArena->free_prev, pArena->free_next );
307 if (pArena->next == arena) break; /* last one */
308 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
310 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
311 break;
314 if (pArena->next == arena)
316 DPRINTF( "*** last block is not marked free\n" );
317 break;
319 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
321 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
322 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
323 break;
325 arena = pArena->next;
330 /***********************************************************************
331 * LocalInit (KERNEL.4)
333 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
335 char *ptr;
336 WORD heapInfoArena, freeArena, lastArena;
337 LOCALHEAPINFO *pHeapInfo;
338 LOCALARENA *pArena, *pFirstArena, *pLastArena;
339 NE_MODULE *pModule;
340 BOOL16 ret = FALSE;
342 /* The initial layout of the heap is: */
343 /* - first arena (FIXED) */
344 /* - heap info structure (FIXED) */
345 /* - large free block (FREE) */
346 /* - last arena (FREE) */
348 TRACE("%04x %04x-%04x\n", selector, start, end);
349 if (!selector) selector = CURRENT_DS;
351 if (TRACE_ON(heap))
353 /* If TRACE_ON(heap) is set, the global heap blocks are */
354 /* cleared before use, so we can test for double initialization. */
355 if (LOCAL_GetHeap(selector))
357 ERR("Heap %04x initialized twice.\n", selector);
358 LOCAL_PrintHeap(selector);
362 if (start == 0)
364 /* start == 0 means: put the local heap at the end of the segment */
366 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
367 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
368 if ( end > 0xfffe ) end = 0xfffe;
369 start -= end;
370 end += start;
372 /* Paranoid check */
374 if ((pModule = NE_GetPtr( GlobalHandle16( selector ) )))
376 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
377 int segNr;
379 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
380 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
381 break;
383 if ( segNr < pModule->seg_count )
385 WORD minsize = pSeg->minsize;
386 if ( pModule->ss == segNr+1 )
387 minsize += pModule->stack_size;
389 TRACE(" new start %04x, minstart: %04x\n", start, minsize);
393 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
395 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
396 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
397 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
398 + sizeof(LOCALHEAPINFO) );
399 lastArena = (end - sizeof(LOCALARENA)) & ~3;
401 /* Make sure there's enough space. */
403 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
405 /* Initialise the first arena */
407 pFirstArena = ARENA_PTR( ptr, start );
408 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
409 pFirstArena->next = heapInfoArena;
410 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
411 pFirstArena->free_prev = start; /* this one */
412 pFirstArena->free_next = freeArena;
414 /* Initialise the arena of the heap info structure */
416 pArena = ARENA_PTR( ptr, heapInfoArena );
417 pArena->prev = start | LOCAL_ARENA_FIXED;
418 pArena->next = freeArena;
420 /* Initialise the heap info structure */
422 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
423 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
424 pHeapInfo->items = 4;
425 pHeapInfo->first = start;
426 pHeapInfo->last = lastArena;
427 pHeapInfo->htable = 0;
428 pHeapInfo->hdelta = 0x20;
429 pHeapInfo->extra = 0x200;
430 pHeapInfo->minsize = lastArena - freeArena;
431 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
433 /* Initialise the large free block */
435 pArena = ARENA_PTR( ptr, freeArena );
436 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
437 pArena->next = lastArena;
438 pArena->size = lastArena - freeArena;
439 pArena->free_prev = start;
440 pArena->free_next = lastArena;
442 /* Initialise the last block */
444 pLastArena = ARENA_PTR( ptr, lastArena );
445 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
446 pLastArena->next = lastArena; /* this one */
447 pLastArena->size = LALIGN(sizeof(LOCALARENA));
448 pLastArena->free_prev = freeArena;
449 pLastArena->free_next = lastArena; /* this one */
451 /* Store the local heap address in the instance data */
453 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
454 LOCAL_PrintHeap( selector );
455 ret = TRUE;
457 done:
458 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
459 return ret;
462 /***********************************************************************
463 * LOCAL_GrowHeap
465 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
467 HANDLE16 hseg;
468 LONG oldsize;
469 LONG end;
470 LOCALHEAPINFO *pHeapInfo;
471 WORD freeArena, lastArena;
472 LOCALARENA *pArena, *pLastArena;
473 char *ptr;
475 hseg = GlobalHandle16( ds );
476 /* maybe mem allocated by Virtual*() ? */
477 if (!hseg) return FALSE;
479 oldsize = GlobalSize16( hseg );
480 /* if nothing can be gained, return */
481 if (oldsize > 0xfff0) return FALSE;
482 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
483 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
484 pHeapInfo = LOCAL_GetHeap( ds );
485 if (pHeapInfo == NULL) {
486 ERR("Heap not found\n" );
487 return FALSE;
489 end = GlobalSize16( hseg );
490 lastArena = (end - sizeof(LOCALARENA)) & ~3;
492 /* Update the HeapInfo */
493 pHeapInfo->items++;
494 freeArena = pHeapInfo->last;
495 pHeapInfo->last = lastArena;
496 pHeapInfo->minsize += end - oldsize;
498 /* grow the old last block */
499 pArena = ARENA_PTR( ptr, freeArena );
500 pArena->size = lastArena - freeArena;
501 pArena->next = lastArena;
502 pArena->free_next = lastArena;
504 /* Initialise the new last block */
506 pLastArena = ARENA_PTR( ptr, lastArena );
507 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
508 pLastArena->next = lastArena; /* this one */
509 pLastArena->size = LALIGN(sizeof(LOCALARENA));
510 pLastArena->free_prev = freeArena;
511 pLastArena->free_next = lastArena; /* this one */
513 /* If block before freeArena is also free then merge them */
514 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
516 LOCAL_RemoveBlock(ptr, freeArena);
517 pHeapInfo->items--;
520 TRACE("Heap expanded\n" );
521 LOCAL_PrintHeap( ds );
522 return TRUE;
526 /***********************************************************************
527 * LOCAL_FreeArena
529 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
531 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
532 LOCALHEAPINFO *pInfo;
533 LOCALARENA *pArena, *pPrev, *pNext;
535 TRACE("%04x ds=%04x\n", arena, ds );
536 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
538 pArena = ARENA_PTR( ptr, arena );
539 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
541 /* shouldn't happen */
542 ERR("Trying to free block %04x twice!\n",
543 arena );
544 LOCAL_PrintHeap( ds );
545 return arena;
548 /* Check if we can merge with the previous block */
550 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
551 pNext = ARENA_PTR( ptr, pArena->next );
552 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
554 arena = pArena->prev & ~3;
555 pArena = pPrev;
556 LOCAL_RemoveBlock( ptr, pPrev->next );
557 pInfo->items--;
559 else /* Make a new free block */
561 LOCAL_MakeBlockFree( ptr, arena );
564 /* Check if we can merge with the next block */
566 if ((pArena->next == pArena->free_next) &&
567 (pArena->next != pInfo->last))
569 LOCAL_RemoveBlock( ptr, pArena->next );
570 pInfo->items--;
572 return 0;
576 /***********************************************************************
577 * LOCAL_ShrinkArena
579 * Shrink an arena by creating a free block at its end if possible.
580 * 'size' includes the arena header, and must be aligned.
582 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
584 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
585 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
587 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
589 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
590 if (!pInfo) return;
591 LOCAL_AddBlock( ptr, arena, arena + size );
592 pInfo->items++;
593 LOCAL_FreeArena( ds, arena + size );
598 /***********************************************************************
599 * LOCAL_GrowArenaDownward
601 * Grow an arena downward by using the previous arena (must be free).
603 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
605 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
606 LOCALHEAPINFO *pInfo;
607 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
608 WORD prevArena = pArena->prev & ~3;
609 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
610 WORD offset, size;
611 char *p;
613 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
614 offset = pPrevArena->size;
615 size = pArena->next - arena - ARENA_HEADER_SIZE;
616 LOCAL_RemoveFreeBlock( ptr, prevArena );
617 LOCAL_RemoveBlock( ptr, arena );
618 pInfo->items--;
619 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
620 while (offset < size)
622 memcpy( p, p + offset, offset );
623 p += offset;
624 size -= offset;
626 if (size) memcpy( p, p + offset, size );
627 LOCAL_ShrinkArena( ds, prevArena, newsize );
632 /***********************************************************************
633 * LOCAL_GrowArenaUpward
635 * Grow an arena upward by using the next arena (must be free and big
636 * enough). Newsize includes the arena header and must be aligned.
638 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
640 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
641 LOCALHEAPINFO *pInfo;
642 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
643 WORD nextArena = pArena->next;
645 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
646 LOCAL_RemoveBlock( ptr, nextArena );
647 pInfo->items--;
648 LOCAL_ShrinkArena( ds, arena, newsize );
652 /***********************************************************************
653 * LOCAL_GetFreeSpace
655 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
657 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
658 LOCALHEAPINFO *pInfo;
659 LOCALARENA *pArena;
660 WORD arena;
661 WORD freespace = 0;
663 if (!(pInfo = LOCAL_GetHeap( ds )))
665 ERR("Local heap not found\n" );
666 LOCAL_PrintHeap(ds);
667 return 0;
669 arena = pInfo->first;
670 pArena = ARENA_PTR( ptr, arena );
671 while (arena != pArena->free_next)
673 arena = pArena->free_next;
674 pArena = ARENA_PTR( ptr, arena );
675 if (pArena->size >= freespace) freespace = pArena->size;
677 /* FIXME doesn't yet calculate space that would become free if everything
678 were discarded when countdiscard == 1 */
679 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
680 else freespace -= ARENA_HEADER_SIZE;
681 return freespace;
685 /***********************************************************************
686 * LOCAL_Compact
688 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
690 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
691 LOCALHEAPINFO *pInfo;
692 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
693 WORD arena, movearena, finalarena, table;
694 WORD count, movesize, size;
695 WORD freespace;
696 LOCALHANDLEENTRY *pEntry;
698 if (!(pInfo = LOCAL_GetHeap( ds )))
700 ERR("Local heap not found\n" );
701 LOCAL_PrintHeap(ds);
702 return 0;
704 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
705 ds, minfree, flags);
706 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
707 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
709 TRACE("Returning %04x.\n", freespace);
710 return freespace;
712 TRACE("Compacting heap %04x.\n", ds);
713 table = pInfo->htable;
714 while(table)
716 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
717 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
719 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
721 /* OK we can move this one if we want */
722 TRACE("handle %04x (block %04x) can be moved.\n",
723 (WORD)((char *)pEntry - ptr), pEntry->addr);
724 movearena = ARENA_HEADER(pEntry->addr);
725 pMoveArena = ARENA_PTR(ptr, movearena);
726 movesize = pMoveArena->next - movearena;
727 arena = pInfo->first;
728 pArena = ARENA_PTR(ptr, arena);
729 size = 0xffff;
730 finalarena = 0;
731 /* Try to find the smallest arena that will do, */
732 /* which is below us in memory */
733 for(;;)
735 arena = pArena->free_next;
736 pArena = ARENA_PTR(ptr, arena);
737 if(arena >= movearena)
738 break;
739 if(arena == pArena->free_next)
740 break;
741 if((pArena->size >= movesize) && (pArena->size < size))
743 size = pArena->size;
744 finalarena = arena;
747 if (finalarena) /* Actually got somewhere to move */
749 TRACE("Moving it to %04x.\n", finalarena);
750 pFinalArena = ARENA_PTR(ptr, finalarena);
751 size = pFinalArena->size;
752 LOCAL_RemoveFreeBlock(ptr, finalarena);
753 LOCAL_ShrinkArena( ds, finalarena, movesize );
754 /* Copy the arena to it's new location */
755 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
756 (char *)pMoveArena + ARENA_HEADER_SIZE,
757 movesize - ARENA_HEADER_SIZE );
758 /* Free the old location */
759 LOCAL_FreeArena(ds, movearena);
760 if (pInfo->notify)
761 LOCAL_CallTo16_word_www(pInfo->notify, LN_MOVE,
762 (WORD)((char *)pEntry - ptr), pEntry->addr);
763 /* Update handle table entry */
764 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
766 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
767 == LOCAL_ARENA_FREE)
769 /* Previous arena is free (but < movesize) */
770 /* so we can 'slide' movearena down into it */
771 finalarena = pMoveArena->prev & ~3;
772 LOCAL_GrowArenaDownward( ds, movearena, movesize );
773 /* Update handle table entry */
774 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
778 table = *(WORD *)pEntry;
780 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
781 if(freespace >= minfree || (flags & LMEM_NODISCARD))
783 TRACE("Returning %04x.\n", freespace);
784 return freespace;
787 table = pInfo->htable;
788 while(table)
790 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
791 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
793 if(pEntry->addr && pEntry->lock == 0 &&
794 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
796 TRACE("Discarding handle %04x (block %04x).\n",
797 (char *)pEntry - ptr, pEntry->addr);
798 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
799 if (pInfo->notify)
800 LOCAL_CallTo16_word_www(pInfo->notify, LN_DISCARD,
801 (char *)pEntry - ptr, pEntry->flags);
802 pEntry->addr = 0;
803 pEntry->flags = (LMEM_DISCARDED >> 8);
806 table = *(WORD *)pEntry;
808 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
812 /***********************************************************************
813 * LOCAL_FindFreeBlock
815 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
817 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
818 LOCALHEAPINFO *pInfo;
819 LOCALARENA *pArena;
820 WORD arena;
822 if (!(pInfo = LOCAL_GetHeap( ds )))
824 ERR("Local heap not found\n" );
825 LOCAL_PrintHeap(ds);
826 return 0;
829 arena = pInfo->first;
830 pArena = ARENA_PTR( ptr, arena );
831 for (;;) {
832 arena = pArena->free_next;
833 pArena = ARENA_PTR( ptr, arena );
834 if (arena == pArena->free_next) break;
835 if (pArena->size >= size) return arena;
837 TRACE("not enough space\n" );
838 LOCAL_PrintHeap(ds);
839 return 0;
843 /***********************************************************************
844 * get_heap_name
846 static const char *get_heap_name( WORD ds )
848 HINSTANCE16 inst = LoadLibrary16( "GDI" );
849 if (ds == GlobalHandleToSel16( inst ))
851 FreeLibrary16( inst );
852 return "GDI";
854 FreeLibrary16( inst );
855 inst = LoadLibrary16( "USER" );
856 if (ds == GlobalHandleToSel16( inst ))
858 FreeLibrary16( inst );
859 return "USER";
861 FreeLibrary16( inst );
862 return "local";
865 /***********************************************************************
866 * LOCAL_GetBlock
867 * The segment may get moved around in this function, so all callers
868 * should reset their pointer variables.
870 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
872 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
873 LOCALHEAPINFO *pInfo;
874 LOCALARENA *pArena;
875 WORD arena;
877 if (!(pInfo = LOCAL_GetHeap( ds )))
879 ERR("Local heap not found\n");
880 LOCAL_PrintHeap(ds);
881 return 0;
884 size += ARENA_HEADER_SIZE;
885 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
887 #if 0
888 notify_done:
889 #endif
890 /* Find a suitable free block */
891 arena = LOCAL_FindFreeBlock( ds, size );
892 if (arena == 0) {
893 /* no space: try to make some */
894 LOCAL_Compact( ds, size, flags );
895 arena = LOCAL_FindFreeBlock( ds, size );
897 if (arena == 0) {
898 /* still no space: try to grow the segment */
899 if (!(LOCAL_GrowHeap( ds )))
901 #if 0
902 /* FIXME: doesn't work correctly yet */
903 if ((pInfo->notify) && (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds - 20, size))) /* FIXME: "size" correct ? (should indicate bytes needed) */
904 goto notify_done;
905 #endif
906 return 0;
908 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
909 pInfo = LOCAL_GetHeap( ds );
910 arena = LOCAL_FindFreeBlock( ds, size );
912 if (arena == 0) {
913 ERR( "not enough space in %s heap %04x for %d bytes\n",
914 get_heap_name(ds), ds, size );
915 #if 0
916 if ((pInfo->notify) &&
917 /* FIXME: "size" correct ? (should indicate bytes needed) */
918 (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds, size)))
919 goto notify_done;
920 #endif
921 return 0;
924 /* Make a block out of the free arena */
925 pArena = ARENA_PTR( ptr, arena );
926 TRACE("size = %04x, arena %04x size %04x\n",
927 size, arena, pArena->size );
928 LOCAL_RemoveFreeBlock( ptr, arena );
929 LOCAL_ShrinkArena( ds, arena, size );
931 if (flags & LMEM_ZEROINIT)
932 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
933 return arena + ARENA_HEADER_SIZE;
937 /***********************************************************************
938 * LOCAL_NewHTable
940 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
942 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
943 LOCALHEAPINFO *pInfo;
944 LOCALHANDLEENTRY *pEntry;
945 HLOCAL16 handle;
946 int i;
948 TRACE("\n" );
949 if (!(pInfo = LOCAL_GetHeap( ds )))
951 ERR("Local heap not found\n");
952 LOCAL_PrintHeap(ds);
953 return FALSE;
956 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
957 + 2 * sizeof(WORD), LMEM_FIXED )))
958 return FALSE;
959 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
960 ERR("ptr == NULL after GetBlock.\n");
961 if (!(pInfo = LOCAL_GetHeap( ds )))
962 ERR("pInfo == NULL after GetBlock.\n");
964 /* Fill the entry table */
966 *(WORD *)(ptr + handle) = pInfo->hdelta;
967 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
968 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
969 pEntry->lock = pEntry->flags = 0xff;
970 pEntry->addr = 0;
972 *(WORD *)pEntry = pInfo->htable;
973 pInfo->htable = handle;
974 return TRUE;
978 /***********************************************************************
979 * LOCAL_GetNewHandleEntry
981 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
983 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
984 LOCALHEAPINFO *pInfo;
985 LOCALHANDLEENTRY *pEntry = NULL;
986 WORD table;
988 if (!(pInfo = LOCAL_GetHeap( ds )))
990 ERR("Local heap not found\n");
991 LOCAL_PrintHeap(ds);
992 return 0;
995 /* Find a free slot in existing tables */
997 table = pInfo->htable;
998 while (table)
1000 WORD count = *(WORD *)(ptr + table);
1001 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1002 for (; count > 0; count--, pEntry++)
1003 if (pEntry->lock == 0xff) break;
1004 if (count) break;
1005 table = *(WORD *)pEntry;
1008 if (!table) /* We need to create a new table */
1010 if (!LOCAL_NewHTable( ds )) return 0;
1011 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1012 pInfo = LOCAL_GetHeap( ds );
1013 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1016 /* Now allocate this entry */
1018 pEntry->lock = 0;
1019 pEntry->flags = 0;
1020 TRACE("(%04x): %04x\n",
1021 ds, ((char *)pEntry - ptr) );
1022 return (HLOCAL16)((char *)pEntry - ptr);
1026 /***********************************************************************
1027 * LOCAL_FreeHandleEntry
1029 * Free a handle table entry.
1031 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1033 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1034 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1035 LOCALHEAPINFO *pInfo;
1036 WORD *pTable;
1037 WORD table, count, i;
1039 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1041 /* Find the table where this handle comes from */
1043 pTable = &pInfo->htable;
1044 while (*pTable)
1046 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1047 if ((handle >= *pTable + sizeof(WORD)) &&
1048 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1049 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1051 if (!*pTable)
1053 ERR("Invalid entry %04x\n", handle);
1054 LOCAL_PrintHeap( ds );
1055 return;
1058 /* Make the entry free */
1060 pEntry->addr = 0; /* just in case */
1061 pEntry->lock = 0xff;
1062 pEntry->flags = 0xff;
1063 /* Now check if all entries in this table are free */
1065 table = *pTable;
1066 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1067 count = *(WORD *)(ptr + table);
1068 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1070 /* Remove the table from the linked list and free it */
1072 TRACE("(%04x): freeing table %04x\n",
1073 ds, table);
1074 *pTable = *(WORD *)pEntry;
1075 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1079 /***********************************************************************
1080 * LOCAL_Free
1082 * Implementation of LocalFree().
1084 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1086 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1088 TRACE("%04x ds=%04x\n", handle, ds );
1090 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1091 if (HANDLE_FIXED( handle ))
1093 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1094 else return handle; /* couldn't free it */
1096 else
1098 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1099 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1101 TRACE("real block at %04x\n",
1102 pEntry->addr );
1103 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr) ))
1104 return handle; /* couldn't free it */
1106 LOCAL_FreeHandleEntry( ds, handle );
1107 return 0; /* OK */
1112 /***********************************************************************
1113 * LOCAL_Alloc
1115 * Implementation of LocalAlloc().
1118 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1120 char *ptr;
1121 HLOCAL16 handle;
1123 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1125 if(size > 0 && size <= 4) size = 5;
1126 if (flags & LMEM_MOVEABLE)
1128 LOCALHANDLEENTRY *plhe;
1129 HLOCAL16 hmem;
1131 if(size)
1133 if (!(hmem = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags )))
1134 return 0;
1136 else /* We just need to allocate a discarded handle */
1137 hmem = 0;
1138 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1140 WARN("Couldn't get handle.\n");
1141 if(hmem)
1142 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1143 return 0;
1145 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1146 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1147 plhe->lock = 0;
1148 if(hmem)
1150 plhe->addr = hmem + sizeof(HLOCAL16);
1151 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1152 *(HLOCAL16 *)(ptr + hmem) = handle;
1154 else
1156 plhe->addr = 0;
1157 plhe->flags = LMEM_DISCARDED >> 8;
1160 else /* FIXED */
1162 if(!size)
1163 return 0;
1164 handle = LOCAL_GetBlock( ds, size, flags );
1166 return handle;
1170 /***********************************************************************
1171 * LOCAL_ReAlloc
1173 * Implementation of LocalReAlloc().
1175 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1177 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1178 LOCALHEAPINFO *pInfo;
1179 LOCALARENA *pArena, *pNext;
1180 LOCALHANDLEENTRY *pEntry = NULL;
1181 WORD arena, oldsize;
1182 HLOCAL16 hmem, blockhandle;
1183 LONG nextarena;
1185 if (!handle) return 0;
1186 if(HANDLE_MOVEABLE(handle) &&
1187 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1188 return 0;
1190 TRACE("%04x %d %04x ds=%04x\n",
1191 handle, size, flags, ds );
1192 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1194 if (HANDLE_FIXED( handle ))
1195 blockhandle = handle;
1196 else
1198 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1199 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1201 HLOCAL16 hl;
1202 if(pEntry->addr)
1203 WARN("Dicarded block has non-zero addr.\n");
1204 TRACE("ReAllocating discarded block\n");
1205 if(size <= 4) size = 5;
1206 if (!(hl = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags)))
1207 return 0;
1208 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1209 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1210 pEntry->addr = hl + sizeof(HLOCAL16);
1211 pEntry->flags = 0;
1212 pEntry->lock = 0;
1213 *(HLOCAL16 *)(ptr + hl) = handle;
1214 return handle;
1216 if (((blockhandle = pEntry->addr) & 3) != 2)
1218 ERR("(%04x,%04x): invalid handle\n",
1219 ds, handle );
1220 return 0;
1222 if(*((HLOCAL16 *)(ptr + blockhandle) - 1) != handle) {
1223 ERR("Back ptr to handle is invalid\n");
1224 return 0;
1228 if (flags & LMEM_MODIFY)
1230 if (HANDLE_MOVEABLE(handle))
1232 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1233 pEntry->flags = (flags & 0x0f00) >> 8;
1234 TRACE("Changing flags to %x.\n", pEntry->flags);
1236 return handle;
1239 if (!size)
1241 if (flags & LMEM_MOVEABLE)
1243 if (HANDLE_FIXED(handle))
1245 TRACE("Freeing fixed block.\n");
1246 return LOCAL_Free( ds, handle );
1248 else /* Moveable block */
1250 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1251 if (pEntry->lock == 0)
1253 /* discards moveable blocks */
1254 TRACE("Discarding block\n");
1255 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
1256 pEntry->addr = 0;
1257 pEntry->flags = (LMEM_DISCARDED >> 8);
1258 return handle;
1261 return 0;
1263 else if(flags == 0)
1265 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1266 if (pEntry->lock == 0)
1268 /* Frees block */
1269 return LOCAL_Free( ds, handle );
1272 return 0;
1275 arena = ARENA_HEADER( blockhandle );
1276 TRACE("arena is %04x\n", arena );
1277 pArena = ARENA_PTR( ptr, arena );
1279 if(size <= 4) size = 5;
1280 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1281 nextarena = LALIGN(blockhandle + size);
1283 /* Check for size reduction */
1285 if (nextarena <= pArena->next)
1287 TRACE("size reduction, making new free block\n");
1288 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1289 TRACE("returning %04x\n", handle );
1290 return handle;
1293 /* Check if the next block is free and large enough */
1295 pNext = ARENA_PTR( ptr, pArena->next );
1296 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1297 (nextarena <= pNext->next))
1299 TRACE("size increase, making new free block\n");
1300 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1301 TRACE("returning %04x\n", handle );
1302 return handle;
1305 /* Now we have to allocate a new block, but not if (fixed block or locked
1306 block) and no LMEM_MOVEABLE */
1308 if (!(flags & LMEM_MOVEABLE))
1310 if (HANDLE_FIXED(handle))
1312 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1313 return 0;
1315 else
1317 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1319 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1320 return 0;
1324 if(HANDLE_MOVEABLE(handle)) size += sizeof(HLOCAL16);
1325 hmem = LOCAL_GetBlock( ds, size, flags );
1326 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1327 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1328 { /* a compaction, which might in turn have */
1329 blockhandle = pEntry->addr ; /* moved the very block we are resizing */
1330 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1332 if (!hmem)
1334 /* Remove the block from the heap and try again */
1335 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1336 if (!buffer) return 0;
1337 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1338 LOCAL_FreeArena( ds, arena );
1339 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1341 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1343 ERR("Can't restore saved block\n" );
1344 HeapFree( GetProcessHeap(), 0, buffer );
1345 return 0;
1347 size = oldsize;
1349 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1350 memcpy( ptr + hmem, buffer, oldsize );
1351 HeapFree( GetProcessHeap(), 0, buffer );
1353 else
1355 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1356 LOCAL_FreeArena( ds, arena );
1358 if (HANDLE_MOVEABLE( handle ))
1360 TRACE("fixing handle\n");
1361 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1362 pEntry->addr = hmem + sizeof(HLOCAL16);
1363 /* Back ptr should still be correct */
1364 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1365 ERR("back ptr is invalid.\n");
1366 hmem = handle;
1368 if (size == oldsize) hmem = 0; /* Realloc failed */
1369 TRACE("returning %04x\n", hmem );
1370 return hmem;
1374 /***********************************************************************
1375 * LOCAL_InternalLock
1377 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1379 HLOCAL16 old_handle = handle;
1381 if (HANDLE_MOVEABLE(handle))
1383 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1384 if (pEntry->flags == LMEM_DISCARDED) return 0;
1385 if (pEntry->lock < 0xfe) pEntry->lock++;
1386 handle = pEntry->addr;
1388 TRACE("%04x returning %04x\n",
1389 old_handle, handle );
1390 return handle;
1394 /***********************************************************************
1395 * LOCAL_Lock
1397 void *LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1399 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1400 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1404 /***********************************************************************
1405 * LOCAL_Unlock
1407 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1409 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1411 TRACE("%04x\n", handle );
1412 if (HANDLE_MOVEABLE(handle))
1414 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1415 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1416 /* For moveable block, return the new lock count */
1417 /* (see _Windows_Internals_ p. 197) */
1418 return --pEntry->lock;
1420 else return FALSE;
1424 /***********************************************************************
1425 * LOCAL_Size
1427 * Implementation of LocalSize().
1429 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1431 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1432 LOCALARENA *pArena;
1434 TRACE("%04x ds=%04x\n", handle, ds );
1436 if (HANDLE_MOVEABLE( handle )) handle = *(WORD *)(ptr + handle);
1437 if (!handle) return 0;
1438 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1439 return pArena->next - handle;
1443 /***********************************************************************
1444 * LOCAL_Flags
1446 * Implementation of LocalFlags().
1448 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1450 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1452 if (HANDLE_MOVEABLE(handle))
1454 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1455 TRACE("(%04x,%04x): returning %04x\n",
1456 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1457 return pEntry->lock | (pEntry->flags << 8);
1459 else
1461 TRACE("(%04x,%04x): returning 0\n",
1462 ds, handle );
1463 return 0;
1468 /***********************************************************************
1469 * LOCAL_HeapSize
1471 * Implementation of LocalHeapSize().
1473 WORD LOCAL_HeapSize( HANDLE16 ds )
1475 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1476 if (!pInfo) return 0;
1477 return pInfo->last - pInfo->first;
1481 /***********************************************************************
1482 * LOCAL_CountFree
1484 * Implementation of LocalCountFree().
1486 WORD LOCAL_CountFree( HANDLE16 ds )
1488 WORD arena, total;
1489 LOCALARENA *pArena;
1490 LOCALHEAPINFO *pInfo;
1491 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1493 if (!(pInfo = LOCAL_GetHeap( ds )))
1495 ERR("(%04x): Local heap not found\n", ds );
1496 LOCAL_PrintHeap( ds );
1497 return 0;
1500 total = 0;
1501 arena = pInfo->first;
1502 pArena = ARENA_PTR( ptr, arena );
1503 for (;;)
1505 arena = pArena->free_next;
1506 pArena = ARENA_PTR( ptr, arena );
1507 if (arena == pArena->free_next) break;
1508 total += pArena->size;
1510 TRACE("(%04x): returning %d\n", ds, total);
1511 return total;
1515 /***********************************************************************
1516 * LOCAL_Handle
1518 * Implementation of LocalHandle().
1520 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1522 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1523 LOCALHEAPINFO *pInfo;
1524 WORD table;
1526 if (!(pInfo = LOCAL_GetHeap( ds )))
1528 ERR("(%04x): Local heap not found\n", ds );
1529 LOCAL_PrintHeap( ds );
1530 return 0;
1533 /* Find the address in the entry tables */
1535 table = pInfo->htable;
1536 while (table)
1538 WORD count = *(WORD *)(ptr + table);
1539 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1540 for (; count > 0; count--, pEntry++)
1541 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1542 table = *(WORD *)pEntry;
1545 return (HLOCAL16)addr; /* Fixed block handle is addr */
1549 /***********************************************************************
1550 * LocalAlloc16 (KERNEL.5)
1552 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1554 HLOCAL16 ret = LOCAL_Alloc( CURRENT_DS, flags, size );
1555 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
1556 return ret;
1560 /***********************************************************************
1561 * LocalReAlloc16 (KERNEL.6)
1563 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1565 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1569 /***********************************************************************
1570 * LocalFree16 (KERNEL.7)
1572 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1574 return LOCAL_Free( CURRENT_DS, handle );
1578 /***********************************************************************
1579 * LocalLock16 (KERNEL.8)
1581 * Note: only the offset part of the pointer is returned by the relay code.
1583 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1585 WORD ds = CURRENT_DS;
1586 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1587 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1591 /***********************************************************************
1592 * LocalUnlock16 (KERNEL.9)
1594 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1596 return LOCAL_Unlock( CURRENT_DS, handle );
1600 /***********************************************************************
1601 * LocalSize16 (KERNEL.10)
1603 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1605 return LOCAL_Size( CURRENT_DS, handle );
1609 /***********************************************************************
1610 * LocalHandle16 (KERNEL.11)
1612 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1614 return LOCAL_Handle( CURRENT_DS, addr );
1618 /***********************************************************************
1619 * LocalFlags16 (KERNEL.12)
1621 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1623 return LOCAL_Flags( CURRENT_DS, handle );
1627 /***********************************************************************
1628 * LocalCompact16 (KERNEL.13)
1630 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1632 TRACE("%04x\n", minfree );
1633 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1637 /***********************************************************************
1638 * LocalNotify (KERNEL.14)
1640 * Installs a callback function that is called for local memory events
1641 * Callback function prototype is
1642 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1643 * wMsg:
1644 * - LN_OUTOFMEM
1645 * NotifyFunc seems to be responsible for allocating some memory,
1646 * returns TRUE for success.
1647 * wArg = number of bytes needed additionally
1648 * - LN_MOVE
1649 * hMem = handle; wArg = old mem location
1650 * - LN_DISCARD
1651 * NotifyFunc seems to be strongly encouraged to return TRUE,
1652 * otherwise LogError() gets called.
1653 * hMem = handle; wArg = flags
1655 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1657 LOCALHEAPINFO *pInfo;
1658 FARPROC16 oldNotify;
1659 HANDLE16 ds = CURRENT_DS;
1661 if (!(pInfo = LOCAL_GetHeap( ds )))
1663 ERR("(%04x): Local heap not found\n", ds );
1664 LOCAL_PrintHeap( ds );
1665 return 0;
1667 TRACE("(%04x): %08lx\n", ds, (DWORD)func );
1668 FIXME("Half implemented\n");
1669 oldNotify = pInfo->notify;
1670 pInfo->notify = func;
1671 return oldNotify;
1675 /***********************************************************************
1676 * LocalShrink16 (KERNEL.121)
1678 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1680 TRACE("%04x %04x\n", handle, newsize );
1681 return 0;
1685 /***********************************************************************
1686 * GetHeapSpaces (KERNEL.138)
1688 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1690 NE_MODULE *pModule;
1691 WORD ds;
1693 if (!(pModule = NE_GetPtr( module ))) return 0;
1694 ds =
1695 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1696 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1700 /***********************************************************************
1701 * LocalCountFree (KERNEL.161)
1703 WORD WINAPI LocalCountFree16(void)
1705 return LOCAL_CountFree( CURRENT_DS );
1709 /***********************************************************************
1710 * LocalHeapSize (KERNEL.162)
1712 WORD WINAPI LocalHeapSize16(void)
1714 TRACE("(void)\n" );
1715 return LOCAL_HeapSize( CURRENT_DS );
1719 /***********************************************************************
1720 * LocalHandleDelta (KERNEL.310)
1722 WORD WINAPI LocalHandleDelta16( WORD delta )
1724 LOCALHEAPINFO *pInfo;
1726 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1728 ERR("Local heap not found\n");
1729 LOCAL_PrintHeap( CURRENT_DS );
1730 return 0;
1732 if (delta) pInfo->hdelta = delta;
1733 TRACE("returning %04x\n", pInfo->hdelta);
1734 return pInfo->hdelta;
1738 /***********************************************************************
1739 * LocalInfo (TOOLHELP.56)
1741 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1743 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle)));
1744 if (!pInfo) return FALSE;
1745 pLocalInfo->wcItems = pInfo->items;
1746 return TRUE;
1750 /***********************************************************************
1751 * LocalFirst (TOOLHELP.57)
1753 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1755 WORD ds = GlobalHandleToSel16( handle );
1756 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1757 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1758 if (!pInfo) return FALSE;
1760 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1761 pLocalEntry->wAddress = pLocalEntry->hHandle;
1762 pLocalEntry->wFlags = LF_FIXED;
1763 pLocalEntry->wcLock = 0;
1764 pLocalEntry->wType = LT_NORMAL;
1765 pLocalEntry->hHeap = handle;
1766 pLocalEntry->wHeapType = NORMAL_HEAP;
1767 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1768 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1769 return TRUE;
1773 /***********************************************************************
1774 * LocalNext (TOOLHELP.58)
1776 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1778 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1779 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1780 LOCALARENA *pArena;
1782 if (!LOCAL_GetHeap( ds )) return FALSE;
1783 if (!pLocalEntry->wNext) return FALSE;
1784 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1786 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1787 pLocalEntry->wAddress = pLocalEntry->hHandle;
1788 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1789 pLocalEntry->wcLock = 0;
1790 pLocalEntry->wType = LT_NORMAL;
1791 if (pArena->next != pLocalEntry->wNext) /* last one? */
1792 pLocalEntry->wNext = pArena->next;
1793 else
1794 pLocalEntry->wNext = 0;
1795 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1796 return TRUE;
1800 /***********************************************************************
1801 * LocalAlloc (KERNEL32.371)
1802 * RETURNS
1803 * Handle: Success
1804 * NULL: Failure
1806 HLOCAL WINAPI LocalAlloc(
1807 UINT flags, /* [in] Allocation attributes */
1808 DWORD size /* [in] Number of bytes to allocate */
1810 return (HLOCAL)GlobalAlloc( flags, size );
1814 /***********************************************************************
1815 * LocalCompact (KERNEL32.372)
1817 UINT WINAPI LocalCompact( UINT minfree )
1819 return 0; /* LocalCompact does nothing in Win32 */
1823 /***********************************************************************
1824 * LocalFlags (KERNEL32.374)
1825 * RETURNS
1826 * Value specifying allocation flags and lock count.
1827 * LMEM_INVALID_HANDLE: Failure
1829 UINT WINAPI LocalFlags(
1830 HLOCAL handle /* [in] Handle of memory object */
1832 return GlobalFlags( (HGLOBAL)handle );
1836 /***********************************************************************
1837 * LocalFree (KERNEL32.375)
1838 * RETURNS
1839 * NULL: Success
1840 * Handle: Failure
1842 HLOCAL WINAPI LocalFree(
1843 HLOCAL handle /* [in] Handle of memory object */
1845 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
1849 /***********************************************************************
1850 * LocalHandle (KERNEL32.376)
1851 * RETURNS
1852 * Handle: Success
1853 * NULL: Failure
1855 HLOCAL WINAPI LocalHandle(
1856 LPCVOID ptr /* [in] Address of local memory object */
1858 return (HLOCAL)GlobalHandle( ptr );
1862 /***********************************************************************
1863 * LocalLock (KERNEL32.377)
1864 * Locks a local memory object and returns pointer to the first byte
1865 * of the memory block.
1867 * RETURNS
1868 * Pointer: Success
1869 * NULL: Failure
1871 LPVOID WINAPI LocalLock(
1872 HLOCAL handle /* [in] Address of local memory object */
1874 return GlobalLock( (HGLOBAL)handle );
1878 /***********************************************************************
1879 * LocalReAlloc (KERNEL32.378)
1880 * RETURNS
1881 * Handle: Success
1882 * NULL: Failure
1884 HLOCAL WINAPI LocalReAlloc(
1885 HLOCAL handle, /* [in] Handle of memory object */
1886 DWORD size, /* [in] New size of block */
1887 UINT flags /* [in] How to reallocate object */
1889 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1893 /***********************************************************************
1894 * LocalShrink (KERNEL32.379)
1896 UINT WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1898 return 0; /* LocalShrink does nothing in Win32 */
1902 /***********************************************************************
1903 * LocalSize (KERNEL32.380)
1904 * RETURNS
1905 * Size: Success
1906 * 0: Failure
1908 UINT WINAPI LocalSize(
1909 HLOCAL handle /* [in] Handle of memory object */
1911 return GlobalSize( (HGLOBAL)handle );
1915 /***********************************************************************
1916 * LocalUnlock (KERNEL32.381)
1917 * RETURNS
1918 * TRUE: Object is still locked
1919 * FALSE: Object is unlocked
1921 BOOL WINAPI LocalUnlock(
1922 HLOCAL handle /* [in] Handle of memory object */
1924 return GlobalUnlock( (HGLOBAL)handle );