Fixed format strings.
[wine/wine64.git] / memory / local.c
bloba7f92e71b93c0db0c059bcc3aab25ed0e8c5a342
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 "ldt.h"
19 #include "task.h"
20 #include "global.h"
21 #include "heap.h"
22 #include "instance.h"
23 #include "local.h"
24 #include "module.h"
25 #include "stackframe.h"
26 #include "toolhelp.h"
27 #include "debugtools.h"
28 #include "callback.h"
30 DEFAULT_DEBUG_CHANNEL(local);
31 DECLARE_DEBUG_CHANNEL(heap);
33 typedef struct
35 /* Arena header */
36 WORD prev; /* Previous arena | arena type */
37 WORD next; /* Next arena */
38 /* Start of the memory block or free-list info */
39 WORD size; /* Size of the free block */
40 WORD free_prev; /* Previous free block */
41 WORD free_next; /* Next free block */
42 } LOCALARENA;
44 #define ARENA_HEADER_SIZE 4
45 #define ARENA_HEADER( handle) ( ((handle) & ~3) - ARENA_HEADER_SIZE)
47 /* Arena types (stored in 'prev' field of the arena) */
48 #define LOCAL_ARENA_FREE 0
49 #define LOCAL_ARENA_FIXED 1
51 /* LocalNotify() msgs */
53 #define LN_OUTOFMEM 0
54 #define LN_MOVE 1
55 #define LN_DISCARD 2
57 /* Layout of a handle entry table
59 * WORD count of entries
60 * LOCALHANDLEENTRY[count] entries
61 * WORD near ptr to next table
63 typedef struct
65 WORD addr; /* Address of the MOVEABLE block */
66 BYTE flags; /* Flags for this block */
67 BYTE lock; /* Lock count */
68 } LOCALHANDLEENTRY;
71 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
72 * in case something actually relies on this.
73 * Note the ARENA_HEADER(addr) still produces the desired result ie. 4n - 4
75 * An unused handle has lock = flags = 0xff. In windows addr is that of next
76 * free handle, at the moment in wine we set it to 0.
78 * A discarded block's handle has lock = addr = 0 and flags = 0x40
79 * (LMEM_DISCARDED >> 8)
82 #include "pshpack1.h"
84 typedef struct
86 WORD check; /* 00 Heap checking flag */
87 WORD freeze; /* 02 Heap frozen flag */
88 WORD items; /* 04 Count of items on the heap */
89 WORD first; /* 06 First item of the heap */
90 WORD pad1; /* 08 Always 0 */
91 WORD last; /* 0a Last item of the heap */
92 WORD pad2; /* 0c Always 0 */
93 BYTE ncompact; /* 0e Compactions counter */
94 BYTE dislevel; /* 0f Discard level */
95 DWORD distotal; /* 10 Total bytes discarded */
96 WORD htable; /* 14 Pointer to handle table */
97 WORD hfree; /* 16 Pointer to free handle table */
98 WORD hdelta; /* 18 Delta to expand the handle table */
99 WORD expand; /* 1a Pointer to expand function (unused) */
100 WORD pstat; /* 1c Pointer to status structure (unused) */
101 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
102 WORD lock; /* 22 Lock count for the heap */
103 WORD extra; /* 24 Extra bytes to allocate when expanding */
104 WORD minsize; /* 26 Minimum size of the heap */
105 WORD magic; /* 28 Magic number */
106 } LOCALHEAPINFO;
108 #include "poppack.h"
110 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
112 WORD USER_HeapSel = 0; /* USER heap selector */
114 /* All local heap allocations are aligned on 4-byte boundaries */
115 #define LALIGN(word) (((word) + 3) & ~3)
117 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
118 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
119 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
120 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
122 /* determine whether the handle belongs to a fixed or a moveable block */
123 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
124 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
127 /* ### start build ### */
128 extern WORD CALLBACK LOCAL_CallTo16_word_www(FARPROC16,WORD,HLOCAL16,WORD);
129 /* ### stop build ### */
131 /***********************************************************************
132 * LOCAL_GetHeap
134 * Return a pointer to the local heap, making sure it exists.
136 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
138 LOCALHEAPINFO *pInfo;
139 INSTANCEDATA *ptr = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( ds, 0 );
140 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
141 if (!ptr || !ptr->heap) return NULL;
142 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
144 WARN("Bad pointer\n");
145 return NULL;
147 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
148 if (pInfo->magic != LOCAL_HEAP_MAGIC)
150 WARN("Bad magic\n");
151 return NULL;
153 return pInfo;
157 /***********************************************************************
158 * LOCAL_MakeBlockFree
160 * Make a block free, inserting it in the free-list.
161 * 'block' is the handle of the block arena; 'baseptr' points to
162 * the beginning of the data segment containing the heap.
164 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
166 LOCALARENA *pArena, *pNext;
167 WORD next;
169 /* Mark the block as free */
171 pArena = ARENA_PTR( baseptr, block );
172 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
173 pArena->size = pArena->next - block;
175 /* Find the next free block (last block is always free) */
177 next = pArena->next;
178 for (;;)
180 pNext = ARENA_PTR( baseptr, next );
181 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
182 next = pNext->next;
185 TRACE("%04x, next %04x\n", block, next );
186 /* Insert the free block in the free-list */
188 pArena->free_prev = pNext->free_prev;
189 pArena->free_next = next;
190 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
191 pNext->free_prev = block;
195 /***********************************************************************
196 * LOCAL_RemoveFreeBlock
198 * Remove a block from the free-list.
199 * 'block' is the handle of the block arena; 'baseptr' points to
200 * the beginning of the data segment containing the heap.
202 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
204 /* Mark the block as fixed */
206 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
207 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
209 /* Remove it from the list */
211 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
212 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
216 /***********************************************************************
217 * LOCAL_AddBlock
219 * Insert a new block in the heap.
220 * 'new' is the handle of the new block arena; 'baseptr' points to
221 * the beginning of the data segment containing the heap; 'prev' is
222 * the block before the new one.
224 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
226 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
227 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
229 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
230 pNew->next = pPrev->next;
231 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
232 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
233 pPrev->next = new;
237 /***********************************************************************
238 * LOCAL_RemoveBlock
240 * Remove a block from the heap.
241 * 'block' is the handle of the block arena; 'baseptr' points to
242 * the beginning of the data segment containing the heap.
244 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
246 LOCALARENA *pArena, *pTmp;
248 /* Remove the block from the free-list */
250 TRACE("\n");
251 pArena = ARENA_PTR( baseptr, block );
252 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
253 LOCAL_RemoveFreeBlock( baseptr, block );
255 /* If the previous block is free, expand its size */
257 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
258 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
259 pTmp->size += pArena->next - block;
261 /* Remove the block from the linked list */
263 pTmp->next = pArena->next;
264 pTmp = ARENA_PTR( baseptr, pArena->next );
265 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
269 /***********************************************************************
270 * LOCAL_PrintHeap
272 static void LOCAL_PrintHeap( HANDLE16 ds )
274 char *ptr;
275 LOCALHEAPINFO *pInfo;
276 WORD arena;
278 /* FIXME - the test should be done when calling the function!
279 plus is not clear that we should print this info
280 only when TRACE_ON is on! */
281 if(!TRACE_ON(local)) return;
283 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
284 pInfo = LOCAL_GetHeap( ds );
286 if (!pInfo)
288 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
289 return;
291 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
292 ds, pInfo->first, pInfo->last, pInfo->items );
294 arena = pInfo->first;
295 for (;;)
297 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
298 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
299 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
300 if (arena == pInfo->first)
302 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
303 pArena->size, pArena->free_prev, pArena->free_next );
305 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
307 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
308 pArena->size, pArena->free_prev, pArena->free_next );
309 if (pArena->next == arena) break; /* last one */
310 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
312 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
313 break;
316 if (pArena->next == arena)
318 DPRINTF( "*** last block is not marked free\n" );
319 break;
321 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
323 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
324 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
325 break;
327 arena = pArena->next;
332 /***********************************************************************
333 * LocalInit (KERNEL.4)
335 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
337 char *ptr;
338 WORD heapInfoArena, freeArena, lastArena;
339 LOCALHEAPINFO *pHeapInfo;
340 LOCALARENA *pArena, *pFirstArena, *pLastArena;
341 NE_MODULE *pModule;
342 BOOL16 ret = FALSE;
344 /* The initial layout of the heap is: */
345 /* - first arena (FIXED) */
346 /* - heap info structure (FIXED) */
347 /* - large free block (FREE) */
348 /* - last arena (FREE) */
350 TRACE("%04x %04x-%04x\n", selector, start, end);
351 if (!selector) selector = CURRENT_DS;
353 if (TRACE_ON(heap))
355 /* If TRACE_ON(heap) is set, the global heap blocks are */
356 /* cleared before use, so we can test for double initialization. */
357 if (LOCAL_GetHeap(selector))
359 ERR("Heap %04x initialized twice.\n", selector);
360 LOCAL_PrintHeap(selector);
364 if (start == 0)
366 /* start == 0 means: put the local heap at the end of the segment */
368 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
369 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
370 if ( end > 0xfffe ) end = 0xfffe;
371 start -= end;
372 end += start;
374 /* Paranoid check */
376 if ((pModule = NE_GetPtr( GlobalHandle16( selector ) )))
378 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
379 int segNr;
381 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
382 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
383 break;
385 if ( segNr < pModule->seg_count )
387 WORD minsize = pSeg->minsize;
388 if ( pModule->ss == segNr+1 )
389 minsize += pModule->stack_size;
391 TRACE(" new start %04x, minstart: %04x\n", start, minsize);
395 ptr = PTR_SEG_OFF_TO_LIN( selector, 0 );
397 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
398 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
399 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
400 + sizeof(LOCALHEAPINFO) );
401 lastArena = (end - sizeof(LOCALARENA)) & ~3;
403 /* Make sure there's enough space. */
405 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
407 /* Initialise the first arena */
409 pFirstArena = ARENA_PTR( ptr, start );
410 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
411 pFirstArena->next = heapInfoArena;
412 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
413 pFirstArena->free_prev = start; /* this one */
414 pFirstArena->free_next = freeArena;
416 /* Initialise the arena of the heap info structure */
418 pArena = ARENA_PTR( ptr, heapInfoArena );
419 pArena->prev = start | LOCAL_ARENA_FIXED;
420 pArena->next = freeArena;
422 /* Initialise the heap info structure */
424 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
425 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
426 pHeapInfo->items = 4;
427 pHeapInfo->first = start;
428 pHeapInfo->last = lastArena;
429 pHeapInfo->htable = 0;
430 pHeapInfo->hdelta = 0x20;
431 pHeapInfo->extra = 0x200;
432 pHeapInfo->minsize = lastArena - freeArena;
433 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
435 /* Initialise the large free block */
437 pArena = ARENA_PTR( ptr, freeArena );
438 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
439 pArena->next = lastArena;
440 pArena->size = lastArena - freeArena;
441 pArena->free_prev = start;
442 pArena->free_next = lastArena;
444 /* Initialise the last block */
446 pLastArena = ARENA_PTR( ptr, lastArena );
447 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
448 pLastArena->next = lastArena; /* this one */
449 pLastArena->size = LALIGN(sizeof(LOCALARENA));
450 pLastArena->free_prev = freeArena;
451 pLastArena->free_next = lastArena; /* this one */
453 /* Store the local heap address in the instance data */
455 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
456 LOCAL_PrintHeap( selector );
457 ret = TRUE;
459 done:
460 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
461 return ret;
464 /***********************************************************************
465 * LOCAL_GrowHeap
467 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
469 HANDLE16 hseg;
470 LONG oldsize;
471 LONG end;
472 LOCALHEAPINFO *pHeapInfo;
473 WORD freeArena, lastArena;
474 LOCALARENA *pArena, *pLastArena;
475 char *ptr;
477 hseg = GlobalHandle16( ds );
478 /* maybe mem allocated by Virtual*() ? */
479 if (!hseg) return FALSE;
481 oldsize = GlobalSize16( hseg );
482 /* if nothing can be gained, return */
483 if (oldsize > 0xfff0) return FALSE;
484 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
485 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
486 pHeapInfo = LOCAL_GetHeap( ds );
487 if (pHeapInfo == NULL) {
488 ERR("Heap not found\n" );
489 return FALSE;
491 end = GlobalSize16( hseg );
492 lastArena = (end - sizeof(LOCALARENA)) & ~3;
494 /* Update the HeapInfo */
495 pHeapInfo->items++;
496 freeArena = pHeapInfo->last;
497 pHeapInfo->last = lastArena;
498 pHeapInfo->minsize += end - oldsize;
500 /* grow the old last block */
501 pArena = ARENA_PTR( ptr, freeArena );
502 pArena->size = lastArena - freeArena;
503 pArena->next = lastArena;
504 pArena->free_next = lastArena;
506 /* Initialise the new last block */
508 pLastArena = ARENA_PTR( ptr, lastArena );
509 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
510 pLastArena->next = lastArena; /* this one */
511 pLastArena->size = LALIGN(sizeof(LOCALARENA));
512 pLastArena->free_prev = freeArena;
513 pLastArena->free_next = lastArena; /* this one */
515 /* If block before freeArena is also free then merge them */
516 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
518 LOCAL_RemoveBlock(ptr, freeArena);
519 pHeapInfo->items--;
522 TRACE("Heap expanded\n" );
523 LOCAL_PrintHeap( ds );
524 return TRUE;
528 /***********************************************************************
529 * LOCAL_FreeArena
531 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
533 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
534 LOCALHEAPINFO *pInfo;
535 LOCALARENA *pArena, *pPrev, *pNext;
537 TRACE("%04x ds=%04x\n", arena, ds );
538 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
540 pArena = ARENA_PTR( ptr, arena );
541 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
543 /* shouldn't happen */
544 ERR("Trying to free block %04x twice!\n",
545 arena );
546 LOCAL_PrintHeap( ds );
547 return arena;
550 /* Check if we can merge with the previous block */
552 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
553 pNext = ARENA_PTR( ptr, pArena->next );
554 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
556 arena = pArena->prev & ~3;
557 pArena = pPrev;
558 LOCAL_RemoveBlock( ptr, pPrev->next );
559 pInfo->items--;
561 else /* Make a new free block */
563 LOCAL_MakeBlockFree( ptr, arena );
566 /* Check if we can merge with the next block */
568 if ((pArena->next == pArena->free_next) &&
569 (pArena->next != pInfo->last))
571 LOCAL_RemoveBlock( ptr, pArena->next );
572 pInfo->items--;
574 return 0;
578 /***********************************************************************
579 * LOCAL_ShrinkArena
581 * Shrink an arena by creating a free block at its end if possible.
582 * 'size' includes the arena header, and must be aligned.
584 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
586 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
587 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
589 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
591 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
592 if (!pInfo) return;
593 LOCAL_AddBlock( ptr, arena, arena + size );
594 pInfo->items++;
595 LOCAL_FreeArena( ds, arena + size );
600 /***********************************************************************
601 * LOCAL_GrowArenaDownward
603 * Grow an arena downward by using the previous arena (must be free).
605 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
607 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
608 LOCALHEAPINFO *pInfo;
609 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
610 WORD prevArena = pArena->prev & ~3;
611 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
612 WORD offset, size;
613 char *p;
615 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
616 offset = pPrevArena->size;
617 size = pArena->next - arena - ARENA_HEADER_SIZE;
618 LOCAL_RemoveFreeBlock( ptr, prevArena );
619 LOCAL_RemoveBlock( ptr, arena );
620 pInfo->items--;
621 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
622 while (offset < size)
624 memcpy( p, p + offset, offset );
625 p += offset;
626 size -= offset;
628 if (size) memcpy( p, p + offset, size );
629 LOCAL_ShrinkArena( ds, prevArena, newsize );
634 /***********************************************************************
635 * LOCAL_GrowArenaUpward
637 * Grow an arena upward by using the next arena (must be free and big
638 * enough). Newsize includes the arena header and must be aligned.
640 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
642 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
643 LOCALHEAPINFO *pInfo;
644 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
645 WORD nextArena = pArena->next;
647 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
648 LOCAL_RemoveBlock( ptr, nextArena );
649 pInfo->items--;
650 LOCAL_ShrinkArena( ds, arena, newsize );
654 /***********************************************************************
655 * LOCAL_GetFreeSpace
657 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
659 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
660 LOCALHEAPINFO *pInfo;
661 LOCALARENA *pArena;
662 WORD arena;
663 WORD freespace = 0;
665 if (!(pInfo = LOCAL_GetHeap( ds )))
667 ERR("Local heap not found\n" );
668 LOCAL_PrintHeap(ds);
669 return 0;
671 arena = pInfo->first;
672 pArena = ARENA_PTR( ptr, arena );
673 while (arena != pArena->free_next)
675 arena = pArena->free_next;
676 pArena = ARENA_PTR( ptr, arena );
677 if (pArena->size >= freespace) freespace = pArena->size;
679 /* FIXME doesn't yet calculate space that would become free if everything
680 were discarded when countdiscard == 1 */
681 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
682 else freespace -= ARENA_HEADER_SIZE;
683 return freespace;
687 /***********************************************************************
688 * LOCAL_Compact
690 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
692 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
693 LOCALHEAPINFO *pInfo;
694 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
695 WORD arena, movearena, finalarena, table;
696 WORD count, movesize, size;
697 WORD freespace;
698 LOCALHANDLEENTRY *pEntry;
700 if (!(pInfo = LOCAL_GetHeap( ds )))
702 ERR("Local heap not found\n" );
703 LOCAL_PrintHeap(ds);
704 return 0;
706 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
707 ds, minfree, flags);
708 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
709 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
711 TRACE("Returning %04x.\n", freespace);
712 return freespace;
714 TRACE("Compacting heap %04x.\n", ds);
715 table = pInfo->htable;
716 while(table)
718 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
719 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
721 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
723 /* OK we can move this one if we want */
724 TRACE("handle %04x (block %04x) can be moved.\n",
725 (WORD)((char *)pEntry - ptr), pEntry->addr);
726 movearena = ARENA_HEADER(pEntry->addr);
727 pMoveArena = ARENA_PTR(ptr, movearena);
728 movesize = pMoveArena->next - movearena;
729 arena = pInfo->first;
730 pArena = ARENA_PTR(ptr, arena);
731 size = 0xffff;
732 finalarena = 0;
733 /* Try to find the smallest arena that will do, */
734 /* which is below us in memory */
735 for(;;)
737 arena = pArena->free_next;
738 pArena = ARENA_PTR(ptr, arena);
739 if(arena >= movearena)
740 break;
741 if(arena == pArena->free_next)
742 break;
743 if((pArena->size >= movesize) && (pArena->size < size))
745 size = pArena->size;
746 finalarena = arena;
749 if (finalarena) /* Actually got somewhere to move */
751 TRACE("Moving it to %04x.\n", finalarena);
752 pFinalArena = ARENA_PTR(ptr, finalarena);
753 size = pFinalArena->size;
754 LOCAL_RemoveFreeBlock(ptr, finalarena);
755 LOCAL_ShrinkArena( ds, finalarena, movesize );
756 /* Copy the arena to it's new location */
757 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
758 (char *)pMoveArena + ARENA_HEADER_SIZE,
759 movesize - ARENA_HEADER_SIZE );
760 /* Free the old location */
761 LOCAL_FreeArena(ds, movearena);
762 if (pInfo->notify)
763 LOCAL_CallTo16_word_www(pInfo->notify, LN_MOVE,
764 (WORD)((char *)pEntry - ptr), pEntry->addr);
765 /* Update handle table entry */
766 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
768 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
769 == LOCAL_ARENA_FREE)
771 /* Previous arena is free (but < movesize) */
772 /* so we can 'slide' movearena down into it */
773 finalarena = pMoveArena->prev & ~3;
774 LOCAL_GrowArenaDownward( ds, movearena, movesize );
775 /* Update handle table entry */
776 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
780 table = *(WORD *)pEntry;
782 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
783 if(freespace >= minfree || (flags & LMEM_NODISCARD))
785 TRACE("Returning %04x.\n", freespace);
786 return freespace;
789 table = pInfo->htable;
790 while(table)
792 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
793 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
795 if(pEntry->addr && pEntry->lock == 0 &&
796 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
798 TRACE("Discarding handle %04x (block %04x).\n",
799 (char *)pEntry - ptr, pEntry->addr);
800 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
801 if (pInfo->notify)
802 LOCAL_CallTo16_word_www(pInfo->notify, LN_DISCARD,
803 (char *)pEntry - ptr, pEntry->flags);
804 pEntry->addr = 0;
805 pEntry->flags = (LMEM_DISCARDED >> 8);
808 table = *(WORD *)pEntry;
810 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
814 /***********************************************************************
815 * LOCAL_FindFreeBlock
817 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
819 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
820 LOCALHEAPINFO *pInfo;
821 LOCALARENA *pArena;
822 WORD arena;
824 if (!(pInfo = LOCAL_GetHeap( ds )))
826 ERR("Local heap not found\n" );
827 LOCAL_PrintHeap(ds);
828 return 0;
831 arena = pInfo->first;
832 pArena = ARENA_PTR( ptr, arena );
833 for (;;) {
834 arena = pArena->free_next;
835 pArena = ARENA_PTR( ptr, arena );
836 if (arena == pArena->free_next) break;
837 if (pArena->size >= size) return arena;
839 TRACE("not enough space\n" );
840 LOCAL_PrintHeap(ds);
841 return 0;
845 /***********************************************************************
846 * get_heap_name
848 static const char *get_heap_name( WORD ds )
850 HINSTANCE16 inst = LoadLibrary16( "GDI" );
851 if (ds == GlobalHandleToSel16( inst ))
853 FreeLibrary16( inst );
854 return "GDI";
856 FreeLibrary16( inst );
857 inst = LoadLibrary16( "USER" );
858 if (ds == GlobalHandleToSel16( inst ))
860 FreeLibrary16( inst );
861 return "USER";
863 FreeLibrary16( inst );
864 return "local";
867 /***********************************************************************
868 * LOCAL_GetBlock
869 * The segment may get moved around in this function, so all callers
870 * should reset their pointer variables.
872 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
874 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
875 LOCALHEAPINFO *pInfo;
876 LOCALARENA *pArena;
877 WORD arena;
879 if (!(pInfo = LOCAL_GetHeap( ds )))
881 ERR("Local heap not found\n");
882 LOCAL_PrintHeap(ds);
883 return 0;
886 size += ARENA_HEADER_SIZE;
887 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
889 #if 0
890 notify_done:
891 #endif
892 /* Find a suitable free block */
893 arena = LOCAL_FindFreeBlock( ds, size );
894 if (arena == 0) {
895 /* no space: try to make some */
896 LOCAL_Compact( ds, size, flags );
897 arena = LOCAL_FindFreeBlock( ds, size );
899 if (arena == 0) {
900 /* still no space: try to grow the segment */
901 if (!(LOCAL_GrowHeap( ds )))
903 #if 0
904 /* FIXME: doesn't work correctly yet */
905 if ((pInfo->notify) && (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds - 20, size))) /* FIXME: "size" correct ? (should indicate bytes needed) */
906 goto notify_done;
907 #endif
908 return 0;
910 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
911 pInfo = LOCAL_GetHeap( ds );
912 arena = LOCAL_FindFreeBlock( ds, size );
914 if (arena == 0) {
915 ERR( "not enough space in %s heap %04x for %d bytes\n",
916 get_heap_name(ds), ds, size );
917 #if 0
918 if ((pInfo->notify) &&
919 /* FIXME: "size" correct ? (should indicate bytes needed) */
920 (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds, size)))
921 goto notify_done;
922 #endif
923 return 0;
926 /* Make a block out of the free arena */
927 pArena = ARENA_PTR( ptr, arena );
928 TRACE("size = %04x, arena %04x size %04x\n",
929 size, arena, pArena->size );
930 LOCAL_RemoveFreeBlock( ptr, arena );
931 LOCAL_ShrinkArena( ds, arena, size );
933 if (flags & LMEM_ZEROINIT)
934 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
935 return arena + ARENA_HEADER_SIZE;
939 /***********************************************************************
940 * LOCAL_NewHTable
942 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
944 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
945 LOCALHEAPINFO *pInfo;
946 LOCALHANDLEENTRY *pEntry;
947 HLOCAL16 handle;
948 int i;
950 TRACE("\n" );
951 if (!(pInfo = LOCAL_GetHeap( ds )))
953 ERR("Local heap not found\n");
954 LOCAL_PrintHeap(ds);
955 return FALSE;
958 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
959 + 2 * sizeof(WORD), LMEM_FIXED )))
960 return FALSE;
961 if (!(ptr = PTR_SEG_OFF_TO_LIN( ds, 0 )))
962 ERR("ptr == NULL after GetBlock.\n");
963 if (!(pInfo = LOCAL_GetHeap( ds )))
964 ERR("pInfo == NULL after GetBlock.\n");
966 /* Fill the entry table */
968 *(WORD *)(ptr + handle) = pInfo->hdelta;
969 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
970 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
971 pEntry->lock = pEntry->flags = 0xff;
972 pEntry->addr = 0;
974 *(WORD *)pEntry = pInfo->htable;
975 pInfo->htable = handle;
976 return TRUE;
980 /***********************************************************************
981 * LOCAL_GetNewHandleEntry
983 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
985 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
986 LOCALHEAPINFO *pInfo;
987 LOCALHANDLEENTRY *pEntry = NULL;
988 WORD table;
990 if (!(pInfo = LOCAL_GetHeap( ds )))
992 ERR("Local heap not found\n");
993 LOCAL_PrintHeap(ds);
994 return 0;
997 /* Find a free slot in existing tables */
999 table = pInfo->htable;
1000 while (table)
1002 WORD count = *(WORD *)(ptr + table);
1003 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1004 for (; count > 0; count--, pEntry++)
1005 if (pEntry->lock == 0xff) break;
1006 if (count) break;
1007 table = *(WORD *)pEntry;
1010 if (!table) /* We need to create a new table */
1012 if (!LOCAL_NewHTable( ds )) return 0;
1013 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1014 pInfo = LOCAL_GetHeap( ds );
1015 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1018 /* Now allocate this entry */
1020 pEntry->lock = 0;
1021 pEntry->flags = 0;
1022 TRACE("(%04x): %04x\n",
1023 ds, ((char *)pEntry - ptr) );
1024 return (HLOCAL16)((char *)pEntry - ptr);
1028 /***********************************************************************
1029 * LOCAL_FreeHandleEntry
1031 * Free a handle table entry.
1033 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1035 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1036 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1037 LOCALHEAPINFO *pInfo;
1038 WORD *pTable;
1039 WORD table, count, i;
1041 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1043 /* Find the table where this handle comes from */
1045 pTable = &pInfo->htable;
1046 while (*pTable)
1048 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1049 if ((handle >= *pTable + sizeof(WORD)) &&
1050 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1051 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1053 if (!*pTable)
1055 ERR("Invalid entry %04x\n", handle);
1056 LOCAL_PrintHeap( ds );
1057 return;
1060 /* Make the entry free */
1062 pEntry->addr = 0; /* just in case */
1063 pEntry->lock = 0xff;
1064 pEntry->flags = 0xff;
1065 /* Now check if all entries in this table are free */
1067 table = *pTable;
1068 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1069 count = *(WORD *)(ptr + table);
1070 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1072 /* Remove the table from the linked list and free it */
1074 TRACE("(%04x): freeing table %04x\n",
1075 ds, table);
1076 *pTable = *(WORD *)pEntry;
1077 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1081 /***********************************************************************
1082 * LOCAL_Free
1084 * Implementation of LocalFree().
1086 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1088 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1090 TRACE("%04x ds=%04x\n", handle, ds );
1092 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1093 if (HANDLE_FIXED( handle ))
1095 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1096 else return handle; /* couldn't free it */
1098 else
1100 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1101 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1103 TRACE("real block at %04x\n",
1104 pEntry->addr );
1105 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr) ))
1106 return handle; /* couldn't free it */
1108 LOCAL_FreeHandleEntry( ds, handle );
1109 return 0; /* OK */
1114 /***********************************************************************
1115 * LOCAL_Alloc
1117 * Implementation of LocalAlloc().
1120 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1122 char *ptr;
1123 HLOCAL16 handle;
1125 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1127 if(size > 0 && size <= 4) size = 5;
1128 if (flags & LMEM_MOVEABLE)
1130 LOCALHANDLEENTRY *plhe;
1131 HLOCAL16 hmem;
1133 if(size)
1135 if (!(hmem = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags )))
1136 return 0;
1138 else /* We just need to allocate a discarded handle */
1139 hmem = 0;
1140 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1142 WARN("Couldn't get handle.\n");
1143 if(hmem)
1144 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1145 return 0;
1147 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1148 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1149 plhe->lock = 0;
1150 if(hmem)
1152 plhe->addr = hmem + sizeof(HLOCAL16);
1153 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1154 *(HLOCAL16 *)(ptr + hmem) = handle;
1156 else
1158 plhe->addr = 0;
1159 plhe->flags = LMEM_DISCARDED >> 8;
1162 else /* FIXED */
1164 if(!size)
1165 return 0;
1166 handle = LOCAL_GetBlock( ds, size, flags );
1168 return handle;
1172 /***********************************************************************
1173 * LOCAL_ReAlloc
1175 * Implementation of LocalReAlloc().
1177 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1179 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1180 LOCALHEAPINFO *pInfo;
1181 LOCALARENA *pArena, *pNext;
1182 LOCALHANDLEENTRY *pEntry = NULL;
1183 WORD arena, oldsize;
1184 HLOCAL16 hmem, blockhandle;
1185 LONG nextarena;
1187 if (!handle) return 0;
1188 if(HANDLE_MOVEABLE(handle) &&
1189 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1190 return 0;
1192 TRACE("%04x %d %04x ds=%04x\n",
1193 handle, size, flags, ds );
1194 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1196 if (HANDLE_FIXED( handle ))
1197 blockhandle = handle;
1198 else
1200 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1201 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1203 HLOCAL16 hl;
1204 if(pEntry->addr)
1205 WARN("Dicarded block has non-zero addr.\n");
1206 TRACE("ReAllocating discarded block\n");
1207 if(size <= 4) size = 5;
1208 if (!(hl = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags)))
1209 return 0;
1210 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1211 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1212 pEntry->addr = hl + sizeof(HLOCAL16);
1213 pEntry->flags = 0;
1214 pEntry->lock = 0;
1215 *(HLOCAL16 *)(ptr + hl) = handle;
1216 return handle;
1218 if (((blockhandle = pEntry->addr) & 3) != 2)
1220 ERR("(%04x,%04x): invalid handle\n",
1221 ds, handle );
1222 return 0;
1224 if(*((HLOCAL16 *)(ptr + blockhandle) - 1) != handle) {
1225 ERR("Back ptr to handle is invalid\n");
1226 return 0;
1230 if (flags & LMEM_MODIFY)
1232 if (HANDLE_MOVEABLE(handle))
1234 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1235 pEntry->flags = (flags & 0x0f00) >> 8;
1236 TRACE("Changing flags to %x.\n", pEntry->flags);
1238 return handle;
1241 if (!size)
1243 if (flags & LMEM_MOVEABLE)
1245 if (HANDLE_FIXED(handle))
1247 TRACE("Freeing fixed block.\n");
1248 return LOCAL_Free( ds, handle );
1250 else /* Moveable block */
1252 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1253 if (pEntry->lock == 0)
1255 /* discards moveable blocks */
1256 TRACE("Discarding block\n");
1257 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
1258 pEntry->addr = 0;
1259 pEntry->flags = (LMEM_DISCARDED >> 8);
1260 return handle;
1263 return 0;
1265 else if(flags == 0)
1267 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1268 if (pEntry->lock == 0)
1270 /* Frees block */
1271 return LOCAL_Free( ds, handle );
1274 return 0;
1277 arena = ARENA_HEADER( blockhandle );
1278 TRACE("arena is %04x\n", arena );
1279 pArena = ARENA_PTR( ptr, arena );
1281 if(size <= 4) size = 5;
1282 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1283 nextarena = LALIGN(blockhandle + size);
1285 /* Check for size reduction */
1287 if (nextarena <= pArena->next)
1289 TRACE("size reduction, making new free block\n");
1290 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1291 TRACE("returning %04x\n", handle );
1292 return handle;
1295 /* Check if the next block is free and large enough */
1297 pNext = ARENA_PTR( ptr, pArena->next );
1298 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1299 (nextarena <= pNext->next))
1301 TRACE("size increase, making new free block\n");
1302 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1303 TRACE("returning %04x\n", handle );
1304 return handle;
1307 /* Now we have to allocate a new block, but not if (fixed block or locked
1308 block) and no LMEM_MOVEABLE */
1310 if (!(flags & LMEM_MOVEABLE))
1312 if (HANDLE_FIXED(handle))
1314 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1315 return 0;
1317 else
1319 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1321 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1322 return 0;
1326 if(HANDLE_MOVEABLE(handle)) size += sizeof(HLOCAL16);
1327 hmem = LOCAL_GetBlock( ds, size, flags );
1328 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1329 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1330 { /* a compaction, which might in turn have */
1331 blockhandle = pEntry->addr ; /* moved the very block we are resizing */
1332 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1334 if (!hmem)
1336 /* Remove the block from the heap and try again */
1337 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1338 if (!buffer) return 0;
1339 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1340 LOCAL_FreeArena( ds, arena );
1341 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1343 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1345 ERR("Can't restore saved block\n" );
1346 HeapFree( GetProcessHeap(), 0, buffer );
1347 return 0;
1349 size = oldsize;
1351 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1352 memcpy( ptr + hmem, buffer, oldsize );
1353 HeapFree( GetProcessHeap(), 0, buffer );
1355 else
1357 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1358 LOCAL_FreeArena( ds, arena );
1360 if (HANDLE_MOVEABLE( handle ))
1362 TRACE("fixing handle\n");
1363 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1364 pEntry->addr = hmem + sizeof(HLOCAL16);
1365 /* Back ptr should still be correct */
1366 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1367 ERR("back ptr is invalid.\n");
1368 hmem = handle;
1370 if (size == oldsize) hmem = 0; /* Realloc failed */
1371 TRACE("returning %04x\n", hmem );
1372 return hmem;
1376 /***********************************************************************
1377 * LOCAL_InternalLock
1379 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1381 HLOCAL16 old_handle = handle;
1383 if (HANDLE_MOVEABLE(handle))
1385 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1386 if (pEntry->flags == LMEM_DISCARDED) return 0;
1387 if (pEntry->lock < 0xfe) pEntry->lock++;
1388 handle = pEntry->addr;
1390 TRACE("%04x returning %04x\n",
1391 old_handle, handle );
1392 return handle;
1396 /***********************************************************************
1397 * LOCAL_Lock
1399 LPSTR LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1401 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1402 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1406 /***********************************************************************
1407 * LOCAL_LockSegptr
1409 SEGPTR LOCAL_LockSegptr( HANDLE16 ds, HLOCAL16 handle )
1411 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1412 return PTR_SEG_OFF_TO_SEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1416 /***********************************************************************
1417 * LOCAL_Unlock
1419 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1421 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1423 TRACE("%04x\n", handle );
1424 if (HANDLE_MOVEABLE(handle))
1426 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1427 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1428 /* For moveable block, return the new lock count */
1429 /* (see _Windows_Internals_ p. 197) */
1430 return --pEntry->lock;
1432 else return FALSE;
1436 /***********************************************************************
1437 * LOCAL_Size
1439 * Implementation of LocalSize().
1441 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1443 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1444 LOCALARENA *pArena;
1446 TRACE("%04x ds=%04x\n", handle, ds );
1448 if (HANDLE_MOVEABLE( handle )) handle = *(WORD *)(ptr + handle);
1449 if (!handle) return 0;
1450 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1451 return pArena->next - handle;
1455 /***********************************************************************
1456 * LOCAL_Flags
1458 * Implementation of LocalFlags().
1460 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1462 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1464 if (HANDLE_MOVEABLE(handle))
1466 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1467 TRACE("(%04x,%04x): returning %04x\n",
1468 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1469 return pEntry->lock | (pEntry->flags << 8);
1471 else
1473 TRACE("(%04x,%04x): returning 0\n",
1474 ds, handle );
1475 return 0;
1480 /***********************************************************************
1481 * LOCAL_HeapSize
1483 * Implementation of LocalHeapSize().
1485 WORD LOCAL_HeapSize( HANDLE16 ds )
1487 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1488 if (!pInfo) return 0;
1489 return pInfo->last - pInfo->first;
1493 /***********************************************************************
1494 * LOCAL_CountFree
1496 * Implementation of LocalCountFree().
1498 WORD LOCAL_CountFree( HANDLE16 ds )
1500 WORD arena, total;
1501 LOCALARENA *pArena;
1502 LOCALHEAPINFO *pInfo;
1503 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1505 if (!(pInfo = LOCAL_GetHeap( ds )))
1507 ERR("(%04x): Local heap not found\n", ds );
1508 LOCAL_PrintHeap( ds );
1509 return 0;
1512 total = 0;
1513 arena = pInfo->first;
1514 pArena = ARENA_PTR( ptr, arena );
1515 for (;;)
1517 arena = pArena->free_next;
1518 pArena = ARENA_PTR( ptr, arena );
1519 if (arena == pArena->free_next) break;
1520 total += pArena->size;
1522 TRACE("(%04x): returning %d\n", ds, total);
1523 return total;
1527 /***********************************************************************
1528 * LOCAL_Handle
1530 * Implementation of LocalHandle().
1532 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1534 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1535 LOCALHEAPINFO *pInfo;
1536 WORD table;
1538 if (!(pInfo = LOCAL_GetHeap( ds )))
1540 ERR("(%04x): Local heap not found\n", ds );
1541 LOCAL_PrintHeap( ds );
1542 return 0;
1545 /* Find the address in the entry tables */
1547 table = pInfo->htable;
1548 while (table)
1550 WORD count = *(WORD *)(ptr + table);
1551 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1552 for (; count > 0; count--, pEntry++)
1553 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1554 table = *(WORD *)pEntry;
1557 return (HLOCAL16)addr; /* Fixed block handle is addr */
1561 /***********************************************************************
1562 * LocalAlloc16 (KERNEL.5)
1564 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1566 HLOCAL16 ret = LOCAL_Alloc( CURRENT_DS, flags, size );
1567 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
1568 return ret;
1572 /***********************************************************************
1573 * LocalReAlloc16 (KERNEL.6)
1575 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1577 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1581 /***********************************************************************
1582 * LocalFree16 (KERNEL.7)
1584 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1586 return LOCAL_Free( CURRENT_DS, handle );
1590 /***********************************************************************
1591 * LocalLock16 (KERNEL.8)
1593 * Note: only the offset part of the pointer is returned by the relay code.
1595 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1597 return LOCAL_LockSegptr( CURRENT_DS, handle );
1601 /***********************************************************************
1602 * LocalUnlock16 (KERNEL.9)
1604 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1606 return LOCAL_Unlock( CURRENT_DS, handle );
1610 /***********************************************************************
1611 * LocalSize16 (KERNEL.10)
1613 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1615 return LOCAL_Size( CURRENT_DS, handle );
1619 /***********************************************************************
1620 * LocalHandle16 (KERNEL.11)
1622 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1624 return LOCAL_Handle( CURRENT_DS, addr );
1628 /***********************************************************************
1629 * LocalFlags16 (KERNEL.12)
1631 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1633 return LOCAL_Flags( CURRENT_DS, handle );
1637 /***********************************************************************
1638 * LocalCompact16 (KERNEL.13)
1640 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1642 TRACE("%04x\n", minfree );
1643 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1647 /***********************************************************************
1648 * LocalNotify (KERNEL.14)
1650 * Installs a callback function that is called for local memory events
1651 * Callback function prototype is
1652 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1653 * wMsg:
1654 * - LN_OUTOFMEM
1655 * NotifyFunc seems to be responsible for allocating some memory,
1656 * returns TRUE for success.
1657 * wArg = number of bytes needed additionally
1658 * - LN_MOVE
1659 * hMem = handle; wArg = old mem location
1660 * - LN_DISCARD
1661 * NotifyFunc seems to be strongly encouraged to return TRUE,
1662 * otherwise LogError() gets called.
1663 * hMem = handle; wArg = flags
1665 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1667 LOCALHEAPINFO *pInfo;
1668 FARPROC16 oldNotify;
1669 HANDLE16 ds = CURRENT_DS;
1671 if (!(pInfo = LOCAL_GetHeap( ds )))
1673 ERR("(%04x): Local heap not found\n", ds );
1674 LOCAL_PrintHeap( ds );
1675 return 0;
1677 TRACE("(%04x): %08lx\n", ds, (DWORD)func );
1678 FIXME("Half implemented\n");
1679 oldNotify = pInfo->notify;
1680 pInfo->notify = func;
1681 return oldNotify;
1685 /***********************************************************************
1686 * LocalShrink16 (KERNEL.121)
1688 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1690 TRACE("%04x %04x\n", handle, newsize );
1691 return 0;
1695 /***********************************************************************
1696 * GetHeapSpaces (KERNEL.138)
1698 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1700 NE_MODULE *pModule;
1701 WORD ds;
1703 if (!(pModule = NE_GetPtr( module ))) return 0;
1704 ds =
1705 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1706 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1710 /***********************************************************************
1711 * LocalCountFree (KERNEL.161)
1713 WORD WINAPI LocalCountFree16(void)
1715 return LOCAL_CountFree( CURRENT_DS );
1719 /***********************************************************************
1720 * LocalHeapSize (KERNEL.162)
1722 WORD WINAPI LocalHeapSize16(void)
1724 TRACE("(void)\n" );
1725 return LOCAL_HeapSize( CURRENT_DS );
1729 /***********************************************************************
1730 * LocalHandleDelta (KERNEL.310)
1732 WORD WINAPI LocalHandleDelta16( WORD delta )
1734 LOCALHEAPINFO *pInfo;
1736 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1738 ERR("Local heap not found\n");
1739 LOCAL_PrintHeap( CURRENT_DS );
1740 return 0;
1742 if (delta) pInfo->hdelta = delta;
1743 TRACE("returning %04x\n", pInfo->hdelta);
1744 return pInfo->hdelta;
1748 /***********************************************************************
1749 * LocalInfo (TOOLHELP.56)
1751 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1753 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(WIN16_GlobalLock16(handle)));
1754 if (!pInfo) return FALSE;
1755 pLocalInfo->wcItems = pInfo->items;
1756 return TRUE;
1760 /***********************************************************************
1761 * LocalFirst (TOOLHELP.57)
1763 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1765 WORD ds = GlobalHandleToSel16( handle );
1766 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1767 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1768 if (!pInfo) return FALSE;
1770 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1771 pLocalEntry->wAddress = pLocalEntry->hHandle;
1772 pLocalEntry->wFlags = LF_FIXED;
1773 pLocalEntry->wcLock = 0;
1774 pLocalEntry->wType = LT_NORMAL;
1775 pLocalEntry->hHeap = handle;
1776 pLocalEntry->wHeapType = NORMAL_HEAP;
1777 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1778 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1779 return TRUE;
1783 /***********************************************************************
1784 * LocalNext (TOOLHELP.58)
1786 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1788 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1789 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1790 LOCALARENA *pArena;
1792 if (!LOCAL_GetHeap( ds )) return FALSE;
1793 if (!pLocalEntry->wNext) return FALSE;
1794 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1796 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1797 pLocalEntry->wAddress = pLocalEntry->hHandle;
1798 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1799 pLocalEntry->wcLock = 0;
1800 pLocalEntry->wType = LT_NORMAL;
1801 if (pArena->next != pLocalEntry->wNext) /* last one? */
1802 pLocalEntry->wNext = pArena->next;
1803 else
1804 pLocalEntry->wNext = 0;
1805 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1806 return TRUE;
1810 /***********************************************************************
1811 * LocalAlloc (KERNEL32.371)
1812 * RETURNS
1813 * Handle: Success
1814 * NULL: Failure
1816 HLOCAL WINAPI LocalAlloc(
1817 UINT flags, /* [in] Allocation attributes */
1818 DWORD size /* [in] Number of bytes to allocate */
1820 return (HLOCAL)GlobalAlloc( flags, size );
1824 /***********************************************************************
1825 * LocalCompact (KERNEL32.372)
1827 UINT WINAPI LocalCompact( UINT minfree )
1829 return 0; /* LocalCompact does nothing in Win32 */
1833 /***********************************************************************
1834 * LocalFlags (KERNEL32.374)
1835 * RETURNS
1836 * Value specifying allocation flags and lock count.
1837 * LMEM_INVALID_HANDLE: Failure
1839 UINT WINAPI LocalFlags(
1840 HLOCAL handle /* [in] Handle of memory object */
1842 return GlobalFlags( (HGLOBAL)handle );
1846 /***********************************************************************
1847 * LocalFree (KERNEL32.375)
1848 * RETURNS
1849 * NULL: Success
1850 * Handle: Failure
1852 HLOCAL WINAPI LocalFree(
1853 HLOCAL handle /* [in] Handle of memory object */
1855 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
1859 /***********************************************************************
1860 * LocalHandle (KERNEL32.376)
1861 * RETURNS
1862 * Handle: Success
1863 * NULL: Failure
1865 HLOCAL WINAPI LocalHandle(
1866 LPCVOID ptr /* [in] Address of local memory object */
1868 return (HLOCAL)GlobalHandle( ptr );
1872 /***********************************************************************
1873 * LocalLock (KERNEL32.377)
1874 * Locks a local memory object and returns pointer to the first byte
1875 * of the memory block.
1877 * RETURNS
1878 * Pointer: Success
1879 * NULL: Failure
1881 LPVOID WINAPI LocalLock(
1882 HLOCAL handle /* [in] Address of local memory object */
1884 return GlobalLock( (HGLOBAL)handle );
1888 /***********************************************************************
1889 * LocalReAlloc (KERNEL32.378)
1890 * RETURNS
1891 * Handle: Success
1892 * NULL: Failure
1894 HLOCAL WINAPI LocalReAlloc(
1895 HLOCAL handle, /* [in] Handle of memory object */
1896 DWORD size, /* [in] New size of block */
1897 UINT flags /* [in] How to reallocate object */
1899 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1903 /***********************************************************************
1904 * LocalShrink (KERNEL32.379)
1906 UINT WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1908 return 0; /* LocalShrink does nothing in Win32 */
1912 /***********************************************************************
1913 * LocalSize (KERNEL32.380)
1914 * RETURNS
1915 * Size: Success
1916 * 0: Failure
1918 UINT WINAPI LocalSize(
1919 HLOCAL handle /* [in] Handle of memory object */
1921 return GlobalSize( (HGLOBAL)handle );
1925 /***********************************************************************
1926 * LocalUnlock (KERNEL32.381)
1927 * RETURNS
1928 * TRUE: Object is still locked
1929 * FALSE: Object is unlocked
1931 BOOL WINAPI LocalUnlock(
1932 HLOCAL handle /* [in] Handle of memory object */
1934 return GlobalUnlock( (HGLOBAL)handle );