Fixed DPA_LoadStream and improved DPA_Merge.
[wine/multimedia.git] / memory / local.c
blob10c46fc7c35f3f50d481edfc86b284c2073eb6ff
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 DECLARE_DEBUG_CHANNEL(heap)
31 DECLARE_DEBUG_CHANNEL(local)
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 */
113 WORD GDI_HeapSel = 0; /* GDI heap selector */
115 /* All local heap allocations are aligned on 4-byte boundaries */
116 #define LALIGN(word) (((word) + 3) & ~3)
118 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
119 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
120 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
121 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
123 /* determine whether the handle belongs to a fixed or a moveable block */
124 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
125 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
127 /***********************************************************************
128 * LOCAL_GetHeap
130 * Return a pointer to the local heap, making sure it exists.
132 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
134 LOCALHEAPINFO *pInfo;
135 INSTANCEDATA *ptr = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( ds, 0 );
136 TRACE_(local)("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
137 if (!ptr || !ptr->heap) return NULL;
138 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
139 return NULL;
140 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
141 if (pInfo->magic != LOCAL_HEAP_MAGIC) return NULL;
142 return pInfo;
146 /***********************************************************************
147 * LOCAL_MakeBlockFree
149 * Make a block free, inserting it in the free-list.
150 * 'block' is the handle of the block arena; 'baseptr' points to
151 * the beginning of the data segment containing the heap.
153 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
155 LOCALARENA *pArena, *pNext;
156 WORD next;
158 /* Mark the block as free */
160 pArena = ARENA_PTR( baseptr, block );
161 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
162 pArena->size = pArena->next - block;
164 /* Find the next free block (last block is always free) */
166 next = pArena->next;
167 for (;;)
169 pNext = ARENA_PTR( baseptr, next );
170 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
171 next = pNext->next;
174 TRACE_(local)("Local_MakeBlockFree %04x, next %04x\n", block, next );
175 /* Insert the free block in the free-list */
177 pArena->free_prev = pNext->free_prev;
178 pArena->free_next = next;
179 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
180 pNext->free_prev = block;
184 /***********************************************************************
185 * LOCAL_RemoveFreeBlock
187 * Remove a block from the free-list.
188 * 'block' is the handle of the block arena; 'baseptr' points to
189 * the beginning of the data segment containing the heap.
191 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
193 /* Mark the block as fixed */
195 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
196 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
198 /* Remove it from the list */
200 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
201 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
205 /***********************************************************************
206 * LOCAL_AddBlock
208 * Insert a new block in the heap.
209 * 'new' is the handle of the new block arena; 'baseptr' points to
210 * the beginning of the data segment containing the heap; 'prev' is
211 * the block before the new one.
213 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
215 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
216 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
218 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
219 pNew->next = pPrev->next;
220 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
221 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
222 pPrev->next = new;
226 /***********************************************************************
227 * LOCAL_RemoveBlock
229 * Remove a block from the heap.
230 * 'block' is the handle of the block arena; 'baseptr' points to
231 * the beginning of the data segment containing the heap.
233 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
235 LOCALARENA *pArena, *pTmp;
237 /* Remove the block from the free-list */
239 TRACE_(local)("Local_RemoveBlock\n");
240 pArena = ARENA_PTR( baseptr, block );
241 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
242 LOCAL_RemoveFreeBlock( baseptr, block );
244 /* If the previous block is free, expand its size */
246 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
247 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
248 pTmp->size += pArena->next - block;
250 /* Remove the block from the linked list */
252 pTmp->next = pArena->next;
253 pTmp = ARENA_PTR( baseptr, pArena->next );
254 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
258 /***********************************************************************
259 * LOCAL_PrintHeap
261 static void LOCAL_PrintHeap( HANDLE16 ds )
263 char *ptr;
264 LOCALHEAPINFO *pInfo;
265 WORD arena;
267 /* FIXME - the test should be done when calling the function!
268 plus is not clear that we should print this info
269 only when TRACE_ON is on! */
270 if(!TRACE_ON(local)) return;
272 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
273 pInfo = LOCAL_GetHeap( ds );
275 if (!pInfo)
277 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
278 return;
280 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
281 ds, pInfo->first, pInfo->last, pInfo->items );
283 arena = pInfo->first;
284 for (;;)
286 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
287 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
288 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
289 if (arena == pInfo->first)
291 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
292 pArena->size, pArena->free_prev, pArena->free_next );
294 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
296 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
297 pArena->size, pArena->free_prev, pArena->free_next );
298 if (pArena->next == arena) break; /* last one */
299 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
301 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
302 break;
305 if (pArena->next == arena)
307 DPRINTF( "*** last block is not marked free\n" );
308 break;
310 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
312 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
313 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
314 break;
316 arena = pArena->next;
321 /***********************************************************************
322 * LocalInit (KERNEL.4)
324 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
326 char *ptr;
327 WORD heapInfoArena, freeArena, lastArena;
328 LOCALHEAPINFO *pHeapInfo;
329 LOCALARENA *pArena, *pFirstArena, *pLastArena;
330 NE_MODULE *pModule;
332 /* The initial layout of the heap is: */
333 /* - first arena (FIXED) */
334 /* - heap info structure (FIXED) */
335 /* - large free block (FREE) */
336 /* - last arena (FREE) */
338 TRACE_(local)("%04x %04x-%04x\n", selector, start, end);
339 if (!selector) selector = CURRENT_DS;
341 if (TRACE_ON(heap))
343 /* If TRACE_ON(heap) is set, the global heap blocks are */
344 /* cleared before use, so we can test for double initialization. */
345 if (LOCAL_GetHeap(selector))
347 ERR_(local)("Heap %04x initialized twice.\n", selector);
348 LOCAL_PrintHeap(selector);
352 if (start == 0) {
353 /* Check if the segment is the DGROUP of a module */
355 if ((pModule = NE_GetPtr( selector )))
357 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule ) + pModule->dgroup - 1;
358 if (pModule->dgroup && (GlobalHandleToSel16(pSeg->hSeg) == selector))
360 /* We can't just use the simple method of using the value
361 * of minsize + stacksize, since there are programs that
362 * resize the data segment before calling InitTask(). So,
363 * we must put it at the end of the segment */
364 start = GlobalSize16( GlobalHandle16( selector ) );
365 start -= end;
366 end += start;
367 TRACE_(local)(" new start %04x, minstart: %04x\n", start, pSeg->minsize + pModule->stack_size);
371 ptr = PTR_SEG_OFF_TO_LIN( selector, 0 );
373 start = LALIGN( MAX( start, sizeof(INSTANCEDATA) ) );
374 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
375 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
376 + sizeof(LOCALHEAPINFO) );
377 lastArena = (end - sizeof(LOCALARENA)) & ~3;
379 /* Make sure there's enough space. */
381 if (freeArena + sizeof(LOCALARENA) >= lastArena) return FALSE;
383 /* Initialise the first arena */
385 pFirstArena = ARENA_PTR( ptr, start );
386 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
387 pFirstArena->next = heapInfoArena;
388 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
389 pFirstArena->free_prev = start; /* this one */
390 pFirstArena->free_next = freeArena;
392 /* Initialise the arena of the heap info structure */
394 pArena = ARENA_PTR( ptr, heapInfoArena );
395 pArena->prev = start | LOCAL_ARENA_FIXED;
396 pArena->next = freeArena;
398 /* Initialise the heap info structure */
400 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
401 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
402 pHeapInfo->items = 4;
403 pHeapInfo->first = start;
404 pHeapInfo->last = lastArena;
405 pHeapInfo->htable = 0;
406 pHeapInfo->hdelta = 0x20;
407 pHeapInfo->extra = 0x200;
408 pHeapInfo->minsize = lastArena - freeArena;
409 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
411 /* Initialise the large free block */
413 pArena = ARENA_PTR( ptr, freeArena );
414 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
415 pArena->next = lastArena;
416 pArena->size = lastArena - freeArena;
417 pArena->free_prev = start;
418 pArena->free_next = lastArena;
420 /* Initialise the last block */
422 pLastArena = ARENA_PTR( ptr, lastArena );
423 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
424 pLastArena->next = lastArena; /* this one */
425 pLastArena->size = LALIGN(sizeof(LOCALARENA));
426 pLastArena->free_prev = freeArena;
427 pLastArena->free_next = lastArena; /* this one */
429 /* Store the local heap address in the instance data */
431 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
432 LOCAL_PrintHeap( selector );
433 return TRUE;
436 /***********************************************************************
437 * LOCAL_GrowHeap
439 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
441 HANDLE16 hseg;
442 LONG oldsize;
443 LONG end;
444 LOCALHEAPINFO *pHeapInfo;
445 WORD freeArena, lastArena;
446 LOCALARENA *pArena, *pLastArena;
447 char *ptr;
449 hseg = GlobalHandle16( ds );
450 /* maybe mem allocated by Virtual*() ? */
451 if (!hseg) return FALSE;
453 oldsize = GlobalSize16( hseg );
454 /* if nothing can be gained, return */
455 if (oldsize > 0xfff0) return FALSE;
456 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
457 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
458 pHeapInfo = LOCAL_GetHeap( ds );
459 if (pHeapInfo == NULL) {
460 ERR_(local)("Heap not found\n" );
461 return FALSE;
463 end = GlobalSize16( hseg );
464 lastArena = (end - sizeof(LOCALARENA)) & ~3;
466 /* Update the HeapInfo */
467 pHeapInfo->items++;
468 freeArena = pHeapInfo->last;
469 pHeapInfo->last = lastArena;
470 pHeapInfo->minsize += end - oldsize;
472 /* grow the old last block */
473 pArena = ARENA_PTR( ptr, freeArena );
474 pArena->size = lastArena - freeArena;
475 pArena->next = lastArena;
476 pArena->free_next = lastArena;
478 /* Initialise the new last block */
480 pLastArena = ARENA_PTR( ptr, lastArena );
481 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
482 pLastArena->next = lastArena; /* this one */
483 pLastArena->size = LALIGN(sizeof(LOCALARENA));
484 pLastArena->free_prev = freeArena;
485 pLastArena->free_next = lastArena; /* this one */
487 /* If block before freeArena is also free then merge them */
488 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
490 LOCAL_RemoveBlock(ptr, freeArena);
491 pHeapInfo->items--;
494 TRACE_(local)("Heap expanded\n" );
495 LOCAL_PrintHeap( ds );
496 return TRUE;
500 /***********************************************************************
501 * LOCAL_FreeArena
503 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
505 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
506 LOCALHEAPINFO *pInfo;
507 LOCALARENA *pArena, *pPrev, *pNext;
509 TRACE_(local)("%04x ds=%04x\n", arena, ds );
510 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
512 pArena = ARENA_PTR( ptr, arena );
513 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
515 /* shouldn't happen */
516 ERR_(local)("Trying to free block %04x twice!\n",
517 arena );
518 LOCAL_PrintHeap( ds );
519 return arena;
522 /* Check if we can merge with the previous block */
524 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
525 pNext = ARENA_PTR( ptr, pArena->next );
526 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
528 arena = pArena->prev & ~3;
529 pArena = pPrev;
530 LOCAL_RemoveBlock( ptr, pPrev->next );
531 pInfo->items--;
533 else /* Make a new free block */
535 LOCAL_MakeBlockFree( ptr, arena );
538 /* Check if we can merge with the next block */
540 if ((pArena->next == pArena->free_next) &&
541 (pArena->next != pInfo->last))
543 LOCAL_RemoveBlock( ptr, pArena->next );
544 pInfo->items--;
546 return 0;
550 /***********************************************************************
551 * LOCAL_ShrinkArena
553 * Shrink an arena by creating a free block at its end if possible.
554 * 'size' includes the arena header, and must be aligned.
556 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
558 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
559 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
561 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
563 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
564 if (!pInfo) return;
565 LOCAL_AddBlock( ptr, arena, arena + size );
566 pInfo->items++;
567 LOCAL_FreeArena( ds, arena + size );
572 /***********************************************************************
573 * LOCAL_GrowArenaDownward
575 * Grow an arena downward by using the previous arena (must be free).
577 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
579 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
580 LOCALHEAPINFO *pInfo;
581 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
582 WORD prevArena = pArena->prev & ~3;
583 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
584 WORD offset, size;
585 char *p;
587 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
588 offset = pPrevArena->size;
589 size = pArena->next - arena - ARENA_HEADER_SIZE;
590 LOCAL_RemoveFreeBlock( ptr, prevArena );
591 LOCAL_RemoveBlock( ptr, arena );
592 pInfo->items--;
593 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
594 while (offset < size)
596 memcpy( p, p + offset, offset );
597 p += offset;
598 size -= offset;
600 if (size) memcpy( p, p + offset, size );
601 LOCAL_ShrinkArena( ds, prevArena, newsize );
606 /***********************************************************************
607 * LOCAL_GrowArenaUpward
609 * Grow an arena upward by using the next arena (must be free and big
610 * enough). Newsize includes the arena header and must be aligned.
612 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
614 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
615 LOCALHEAPINFO *pInfo;
616 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
617 WORD nextArena = pArena->next;
619 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
620 LOCAL_RemoveBlock( ptr, nextArena );
621 pInfo->items--;
622 LOCAL_ShrinkArena( ds, arena, newsize );
626 /***********************************************************************
627 * LOCAL_GetFreeSpace
629 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
631 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
632 LOCALHEAPINFO *pInfo;
633 LOCALARENA *pArena;
634 WORD arena;
635 WORD freespace = 0;
637 if (!(pInfo = LOCAL_GetHeap( ds )))
639 ERR_(local)("Local heap not found\n" );
640 LOCAL_PrintHeap(ds);
641 return 0;
643 arena = pInfo->first;
644 pArena = ARENA_PTR( ptr, arena );
645 while (arena != pArena->free_next)
647 arena = pArena->free_next;
648 pArena = ARENA_PTR( ptr, arena );
649 if (pArena->size >= freespace) freespace = pArena->size;
651 /* FIXME doesn't yet calculate space that would become free if everything
652 were discarded when countdiscard == 1 */
653 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
654 else freespace -= ARENA_HEADER_SIZE;
655 return freespace;
659 /***********************************************************************
660 * LOCAL_Compact
662 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
664 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
665 LOCALHEAPINFO *pInfo;
666 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
667 WORD arena, movearena, finalarena, table;
668 WORD count, movesize, size;
669 WORD freespace;
670 LOCALHANDLEENTRY *pEntry;
672 if (!(pInfo = LOCAL_GetHeap( ds )))
674 ERR_(local)("Local heap not found\n" );
675 LOCAL_PrintHeap(ds);
676 return 0;
678 TRACE_(local)("ds = %04x, minfree = %04x, flags = %04x\n",
679 ds, minfree, flags);
680 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
681 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
683 TRACE_(local)("Returning %04x.\n", freespace);
684 return freespace;
686 TRACE_(local)("Compacting heap %04x.\n", ds);
687 table = pInfo->htable;
688 while(table)
690 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
691 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
693 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
695 /* OK we can move this one if we want */
696 TRACE_(local)("handle %04x (block %04x) can be moved.\n",
697 (WORD)((char *)pEntry - ptr), pEntry->addr);
698 movearena = ARENA_HEADER(pEntry->addr);
699 pMoveArena = ARENA_PTR(ptr, movearena);
700 movesize = pMoveArena->next - movearena;
701 arena = pInfo->first;
702 pArena = ARENA_PTR(ptr, arena);
703 size = 0xffff;
704 finalarena = 0;
705 /* Try to find the smallest arena that will do, */
706 /* which is below us in memory */
707 for(;;)
709 arena = pArena->free_next;
710 pArena = ARENA_PTR(ptr, arena);
711 if(arena >= movearena)
712 break;
713 if(arena == pArena->free_next)
714 break;
715 if((pArena->size >= movesize) && (pArena->size < size))
717 size = pArena->size;
718 finalarena = arena;
721 if (finalarena) /* Actually got somewhere to move */
723 TRACE_(local)("Moving it to %04x.\n", finalarena);
724 pFinalArena = ARENA_PTR(ptr, finalarena);
725 size = pFinalArena->size;
726 LOCAL_RemoveFreeBlock(ptr, finalarena);
727 LOCAL_ShrinkArena( ds, finalarena, movesize );
728 /* Copy the arena to it's new location */
729 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
730 (char *)pMoveArena + ARENA_HEADER_SIZE,
731 movesize - ARENA_HEADER_SIZE );
732 /* Free the old location */
733 LOCAL_FreeArena(ds, movearena);
734 if (pInfo->notify)
735 Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_MOVE,
736 (WORD)((char *)pEntry - ptr), pEntry->addr);
737 /* Update handle table entry */
738 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
740 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
741 == LOCAL_ARENA_FREE)
743 /* Previous arena is free (but < movesize) */
744 /* so we can 'slide' movearena down into it */
745 finalarena = pMoveArena->prev & ~3;
746 LOCAL_GrowArenaDownward( ds, movearena, movesize );
747 /* Update handle table entry */
748 pEntry->addr = finalarena + ARENA_HEADER_SIZE + sizeof(HLOCAL16) ;
752 table = *(WORD *)pEntry;
754 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
755 if(freespace >= minfree || (flags & LMEM_NODISCARD))
757 TRACE_(local)("Returning %04x.\n", freespace);
758 return freespace;
761 table = pInfo->htable;
762 while(table)
764 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
765 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
767 if(pEntry->addr && pEntry->lock == 0 &&
768 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
770 TRACE_(local)("Discarding handle %04x (block %04x).\n",
771 (char *)pEntry - ptr, pEntry->addr);
772 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
773 if (pInfo->notify)
774 Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_DISCARD,
775 (char *)pEntry - ptr, pEntry->flags);
776 pEntry->addr = 0;
777 pEntry->flags = (LMEM_DISCARDED >> 8);
780 table = *(WORD *)pEntry;
782 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
786 /***********************************************************************
787 * LOCAL_FindFreeBlock
789 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
791 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
792 LOCALHEAPINFO *pInfo;
793 LOCALARENA *pArena;
794 WORD arena;
796 if (!(pInfo = LOCAL_GetHeap( ds )))
798 ERR_(local)("Local heap not found\n" );
799 LOCAL_PrintHeap(ds);
800 return 0;
803 arena = pInfo->first;
804 pArena = ARENA_PTR( ptr, arena );
805 for (;;) {
806 arena = pArena->free_next;
807 pArena = ARENA_PTR( ptr, arena );
808 if (arena == pArena->free_next) break;
809 if (pArena->size >= size) return arena;
811 TRACE_(local)("not enough space\n" );
812 LOCAL_PrintHeap(ds);
813 return 0;
816 /***********************************************************************
817 * LOCAL_GetBlock
818 * The segment may get moved around in this function, so all callers
819 * should reset their pointer variables.
821 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
823 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
824 LOCALHEAPINFO *pInfo;
825 LOCALARENA *pArena;
826 WORD arena;
828 if (!(pInfo = LOCAL_GetHeap( ds )))
830 ERR_(local)("Local heap not found\n");
831 LOCAL_PrintHeap(ds);
832 return 0;
835 size += ARENA_HEADER_SIZE;
836 size = LALIGN( MAX( size, sizeof(LOCALARENA) ) );
838 #if 0
839 notify_done:
840 #endif
841 /* Find a suitable free block */
842 arena = LOCAL_FindFreeBlock( ds, size );
843 if (arena == 0) {
844 /* no space: try to make some */
845 LOCAL_Compact( ds, size, flags );
846 arena = LOCAL_FindFreeBlock( ds, size );
848 if (arena == 0) {
849 /* still no space: try to grow the segment */
850 if (!(LOCAL_GrowHeap( ds )))
852 #if 0
853 /* FIXME: doesn't work correctly yet */
854 if ((pInfo->notify) && (Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_OUTOFMEM, ds - 20, size))) /* FIXME: "size" correct ? (should indicate bytes needed) */
855 goto notify_done;
856 #endif
857 return 0;
859 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
860 pInfo = LOCAL_GetHeap( ds );
861 arena = LOCAL_FindFreeBlock( ds, size );
863 if (arena == 0) {
864 if (ds == GDI_HeapSel) {
865 ERR_(local)("not enough space in GDI local heap "
866 "(%04x) for %d bytes\n", ds, size );
867 } else if (ds == USER_HeapSel) {
868 ERR_(local)("not enough space in USER local heap "
869 "(%04x) for %d bytes\n", ds, size );
870 } else {
871 ERR_(local)("not enough space in local heap "
872 "%04x for %d bytes\n", ds, size );
874 #if 0
875 if ((pInfo->notify) &&
876 /* FIXME: "size" correct ? (should indicate bytes needed) */
877 (Callbacks->CallLocalNotifyFunc(pInfo->notify, LN_OUTOFMEM, ds, size)))
878 goto notify_done;
879 #endif
880 return 0;
883 /* Make a block out of the free arena */
884 pArena = ARENA_PTR( ptr, arena );
885 TRACE_(local)("LOCAL_GetBlock size = %04x, arena %04x size %04x\n",
886 size, arena, pArena->size );
887 LOCAL_RemoveFreeBlock( ptr, arena );
888 LOCAL_ShrinkArena( ds, arena, size );
890 if (flags & LMEM_ZEROINIT)
891 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
892 return arena + ARENA_HEADER_SIZE;
896 /***********************************************************************
897 * LOCAL_NewHTable
899 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
901 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
902 LOCALHEAPINFO *pInfo;
903 LOCALHANDLEENTRY *pEntry;
904 HLOCAL16 handle;
905 int i;
907 TRACE_(local)("Local_NewHTable\n" );
908 if (!(pInfo = LOCAL_GetHeap( ds )))
910 ERR_(local)("Local heap not found\n");
911 LOCAL_PrintHeap(ds);
912 return FALSE;
915 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
916 + 2 * sizeof(WORD), LMEM_FIXED )))
917 return FALSE;
918 if (!(ptr = PTR_SEG_OFF_TO_LIN( ds, 0 )))
919 ERR_(local)("ptr == NULL after GetBlock.\n");
920 if (!(pInfo = LOCAL_GetHeap( ds )))
921 ERR_(local)("pInfo == NULL after GetBlock.\n");
923 /* Fill the entry table */
925 *(WORD *)(ptr + handle) = pInfo->hdelta;
926 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
927 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
928 pEntry->lock = pEntry->flags = 0xff;
929 pEntry->addr = 0;
931 *(WORD *)pEntry = pInfo->htable;
932 pInfo->htable = handle;
933 return TRUE;
937 /***********************************************************************
938 * LOCAL_GetNewHandleEntry
940 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
942 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
943 LOCALHEAPINFO *pInfo;
944 LOCALHANDLEENTRY *pEntry = NULL;
945 WORD table;
947 if (!(pInfo = LOCAL_GetHeap( ds )))
949 ERR_(local)("Local heap not found\n");
950 LOCAL_PrintHeap(ds);
951 return 0;
954 /* Find a free slot in existing tables */
956 table = pInfo->htable;
957 while (table)
959 WORD count = *(WORD *)(ptr + table);
960 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
961 for (; count > 0; count--, pEntry++)
962 if (pEntry->lock == 0xff) break;
963 if (count) break;
964 table = *(WORD *)pEntry;
967 if (!table) /* We need to create a new table */
969 if (!LOCAL_NewHTable( ds )) return 0;
970 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
971 pInfo = LOCAL_GetHeap( ds );
972 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
975 /* Now allocate this entry */
977 pEntry->lock = 0;
978 pEntry->flags = 0;
979 TRACE_(local)("(%04x): %04x\n",
980 ds, ((char *)pEntry - ptr) );
981 return (HLOCAL16)((char *)pEntry - ptr);
985 /***********************************************************************
986 * LOCAL_FreeHandleEntry
988 * Free a handle table entry.
990 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
992 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
993 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
994 LOCALHEAPINFO *pInfo;
995 WORD *pTable;
996 WORD table, count, i;
998 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1000 /* Find the table where this handle comes from */
1002 pTable = &pInfo->htable;
1003 while (*pTable)
1005 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1006 if ((handle >= *pTable + sizeof(WORD)) &&
1007 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1008 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1010 if (!*pTable)
1012 ERR_(local)("Invalid entry %04x\n", handle);
1013 LOCAL_PrintHeap( ds );
1014 return;
1017 /* Make the entry free */
1019 pEntry->addr = 0; /* just in case */
1020 pEntry->lock = 0xff;
1021 pEntry->flags = 0xff;
1022 /* Now check if all entries in this table are free */
1024 table = *pTable;
1025 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1026 count = *(WORD *)(ptr + table);
1027 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1029 /* Remove the table from the linked list and free it */
1031 TRACE_(local)("(%04x): freeing table %04x\n",
1032 ds, table);
1033 *pTable = *(WORD *)pEntry;
1034 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1038 /***********************************************************************
1039 * LOCAL_Free
1041 * Implementation of LocalFree().
1043 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1045 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1047 TRACE_(local)("%04x ds=%04x\n", handle, ds );
1049 if (!handle) { WARN_(local)("Handle is 0.\n" ); return 0; }
1050 if (HANDLE_FIXED( handle ))
1052 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1053 else return handle; /* couldn't free it */
1055 else
1057 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1058 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1060 TRACE_(local)("real block at %04x\n",
1061 pEntry->addr );
1062 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr) ))
1063 return handle; /* couldn't free it */
1065 LOCAL_FreeHandleEntry( ds, handle );
1066 return 0; /* OK */
1071 /***********************************************************************
1072 * LOCAL_Alloc
1074 * Implementation of LocalAlloc().
1077 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1079 char *ptr;
1080 HLOCAL16 handle;
1082 TRACE_(local)("%04x %d ds=%04x\n", flags, size, ds );
1084 if(size > 0 && size <= 4) size = 5;
1085 if (flags & LMEM_MOVEABLE)
1087 LOCALHANDLEENTRY *plhe;
1088 HLOCAL16 hmem;
1090 if(size)
1092 if (!(hmem = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags )))
1093 return 0;
1095 else /* We just need to allocate a discarded handle */
1096 hmem = 0;
1097 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1099 WARN_(local)("Couldn't get handle.\n");
1100 if(hmem)
1101 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1102 return 0;
1104 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1105 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1106 plhe->lock = 0;
1107 if(hmem)
1109 plhe->addr = hmem + sizeof(HLOCAL16);
1110 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1111 *(HLOCAL16 *)(ptr + hmem) = handle;
1113 else
1115 plhe->addr = 0;
1116 plhe->flags = LMEM_DISCARDED >> 8;
1119 else /* FIXED */
1121 if(!size)
1122 return 0;
1123 handle = LOCAL_GetBlock( ds, size, flags );
1125 return handle;
1129 /***********************************************************************
1130 * LOCAL_ReAlloc
1132 * Implementation of LocalReAlloc().
1134 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1136 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1137 LOCALHEAPINFO *pInfo;
1138 LOCALARENA *pArena, *pNext;
1139 LOCALHANDLEENTRY *pEntry = NULL;
1140 WORD arena, oldsize;
1141 HLOCAL16 hmem, blockhandle;
1142 LONG nextarena;
1144 if (!handle) return 0;
1145 if(HANDLE_MOVEABLE(handle) &&
1146 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1147 return 0;
1149 TRACE_(local)("%04x %d %04x ds=%04x\n",
1150 handle, size, flags, ds );
1151 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1153 if (HANDLE_FIXED( handle ))
1154 blockhandle = handle;
1155 else
1157 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1158 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1160 HLOCAL16 hl;
1161 if(pEntry->addr)
1162 WARN_(local)("Dicarded block has non-zero addr.\n");
1163 TRACE_(local)("ReAllocating discarded block\n");
1164 if(size <= 4) size = 5;
1165 if (!(hl = LOCAL_GetBlock( ds, size + sizeof(HLOCAL16), flags)))
1166 return 0;
1167 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1168 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1169 pEntry->addr = hl + sizeof(HLOCAL16);
1170 pEntry->flags = 0;
1171 pEntry->lock = 0;
1172 *(HLOCAL16 *)(ptr + hl) = handle;
1173 return handle;
1175 if (((blockhandle = pEntry->addr) & 3) != 2)
1177 ERR_(local)("(%04x,%04x): invalid handle\n",
1178 ds, handle );
1179 return 0;
1181 if(*((HLOCAL16 *)(ptr + blockhandle) - 1) != handle) {
1182 ERR_(local)("Back ptr to handle is invalid\n");
1183 return 0;
1187 if (flags & LMEM_MODIFY)
1189 if (HANDLE_MOVEABLE(handle))
1191 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1192 pEntry->flags = (flags & 0x0f00) >> 8;
1193 TRACE_(local)("Changing flags to %x.\n", pEntry->flags);
1195 return handle;
1198 if (!size)
1200 if (flags & LMEM_MOVEABLE)
1202 if (HANDLE_FIXED(handle))
1204 TRACE_(local)("Freeing fixed block.\n");
1205 return LOCAL_Free( ds, handle );
1207 else /* Moveable block */
1209 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1210 if (pEntry->lock == 0)
1212 /* discards moveable blocks */
1213 TRACE_(local)("Discarding block\n");
1214 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr));
1215 pEntry->addr = 0;
1216 pEntry->flags = (LMEM_DISCARDED >> 8);
1217 return handle;
1220 return 0;
1222 else if(flags == 0)
1224 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1225 if (pEntry->lock == 0)
1227 /* Frees block */
1228 return LOCAL_Free( ds, handle );
1231 return 0;
1234 arena = ARENA_HEADER( blockhandle );
1235 TRACE_(local)("arena is %04x\n", arena );
1236 pArena = ARENA_PTR( ptr, arena );
1238 if(size <= 4) size = 5;
1239 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1240 nextarena = LALIGN(blockhandle + size);
1242 /* Check for size reduction */
1244 if (nextarena <= pArena->next)
1246 TRACE_(local)("size reduction, making new free block\n");
1247 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1248 TRACE_(local)("returning %04x\n", handle );
1249 return handle;
1252 /* Check if the next block is free and large enough */
1254 pNext = ARENA_PTR( ptr, pArena->next );
1255 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1256 (nextarena <= pNext->next))
1258 TRACE_(local)("size increase, making new free block\n");
1259 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1260 TRACE_(local)("returning %04x\n", handle );
1261 return handle;
1264 /* Now we have to allocate a new block, but not if (fixed block or locked
1265 block) and no LMEM_MOVEABLE */
1267 if (!(flags & LMEM_MOVEABLE))
1269 if (HANDLE_FIXED(handle))
1271 ERR_(local)("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1272 return 0;
1274 else
1276 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1278 ERR_(local)("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1279 return 0;
1283 if(HANDLE_MOVEABLE(handle)) size += sizeof(HLOCAL16);
1284 hmem = LOCAL_GetBlock( ds, size, flags );
1285 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1286 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1287 { /* a compaction, which might in turn have */
1288 blockhandle = pEntry->addr ; /* moved the very block we are resizing */
1289 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1291 if (!hmem)
1293 /* Remove the block from the heap and try again */
1294 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1295 if (!buffer) return 0;
1296 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1297 LOCAL_FreeArena( ds, arena );
1298 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1300 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1302 ERR_(local)("Can't restore saved block\n" );
1303 HeapFree( GetProcessHeap(), 0, buffer );
1304 return 0;
1306 size = oldsize;
1308 ptr = PTR_SEG_OFF_TO_LIN( ds, 0 ); /* Reload ptr */
1309 memcpy( ptr + hmem, buffer, oldsize );
1310 HeapFree( GetProcessHeap(), 0, buffer );
1312 else
1314 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1315 LOCAL_FreeArena( ds, arena );
1317 if (HANDLE_MOVEABLE( handle ))
1319 TRACE_(local)("fixing handle\n");
1320 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1321 pEntry->addr = hmem + sizeof(HLOCAL16);
1322 /* Back ptr should still be correct */
1323 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1324 ERR_(local)("back ptr is invalid.\n");
1325 hmem = handle;
1327 if (size == oldsize) hmem = 0; /* Realloc failed */
1328 TRACE_(local)("returning %04x\n", hmem );
1329 return hmem;
1333 /***********************************************************************
1334 * LOCAL_InternalLock
1336 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1338 HLOCAL16 old_handle = handle;
1340 if (HANDLE_MOVEABLE(handle))
1342 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1343 if (pEntry->flags == LMEM_DISCARDED) return 0;
1344 if (pEntry->lock < 0xfe) pEntry->lock++;
1345 handle = pEntry->addr;
1347 TRACE_(local)("%04x returning %04x\n",
1348 old_handle, handle );
1349 return handle;
1353 /***********************************************************************
1354 * LOCAL_Lock
1356 LPSTR LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1358 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1359 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1363 /***********************************************************************
1364 * LOCAL_LockSegptr
1366 SEGPTR LOCAL_LockSegptr( HANDLE16 ds, HLOCAL16 handle )
1368 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1369 return PTR_SEG_OFF_TO_SEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1373 /***********************************************************************
1374 * LOCAL_Unlock
1376 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1378 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1380 TRACE_(local)("%04x\n", handle );
1381 if (HANDLE_MOVEABLE(handle))
1383 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1384 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1385 /* For moveable block, return the new lock count */
1386 /* (see _Windows_Internals_ p. 197) */
1387 return --pEntry->lock;
1389 else return FALSE;
1393 /***********************************************************************
1394 * LOCAL_Size
1396 * Implementation of LocalSize().
1398 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1400 char *ptr = PTR_SEG_OFF_TO_LIN( CURRENT_DS, 0 );
1401 LOCALARENA *pArena;
1403 TRACE_(local)("%04x ds=%04x\n", handle, ds );
1405 if (HANDLE_MOVEABLE( handle )) handle = *(WORD *)(ptr + handle);
1406 if (!handle) return 0;
1407 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1408 return pArena->next - handle;
1412 /***********************************************************************
1413 * LOCAL_Flags
1415 * Implementation of LocalFlags().
1417 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1419 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1421 if (HANDLE_MOVEABLE(handle))
1423 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1424 TRACE_(local)("(%04x,%04x): returning %04x\n",
1425 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1426 return pEntry->lock | (pEntry->flags << 8);
1428 else
1430 TRACE_(local)("(%04x,%04x): returning 0\n",
1431 ds, handle );
1432 return 0;
1437 /***********************************************************************
1438 * LOCAL_HeapSize
1440 * Implementation of LocalHeapSize().
1442 WORD LOCAL_HeapSize( HANDLE16 ds )
1444 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1445 if (!pInfo) return 0;
1446 return pInfo->last - pInfo->first;
1450 /***********************************************************************
1451 * LOCAL_CountFree
1453 * Implementation of LocalCountFree().
1455 WORD LOCAL_CountFree( HANDLE16 ds )
1457 WORD arena, total;
1458 LOCALARENA *pArena;
1459 LOCALHEAPINFO *pInfo;
1460 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1462 if (!(pInfo = LOCAL_GetHeap( ds )))
1464 ERR_(local)("(%04x): Local heap not found\n", ds );
1465 LOCAL_PrintHeap( ds );
1466 return 0;
1469 total = 0;
1470 arena = pInfo->first;
1471 pArena = ARENA_PTR( ptr, arena );
1472 for (;;)
1474 arena = pArena->free_next;
1475 pArena = ARENA_PTR( ptr, arena );
1476 if (arena == pArena->free_next) break;
1477 total += pArena->size;
1479 TRACE_(local)("(%04x): returning %d\n", ds, total);
1480 return total;
1484 /***********************************************************************
1485 * LOCAL_Handle
1487 * Implementation of LocalHandle().
1489 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1491 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1492 LOCALHEAPINFO *pInfo;
1493 WORD table;
1495 if (!(pInfo = LOCAL_GetHeap( ds )))
1497 ERR_(local)("(%04x): Local heap not found\n", ds );
1498 LOCAL_PrintHeap( ds );
1499 return 0;
1502 /* Find the address in the entry tables */
1504 table = pInfo->htable;
1505 while (table)
1507 WORD count = *(WORD *)(ptr + table);
1508 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1509 for (; count > 0; count--, pEntry++)
1510 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1511 table = *(WORD *)pEntry;
1514 return (HLOCAL16)addr; /* Fixed block handle is addr */
1518 /***********************************************************************
1519 * LocalAlloc16 (KERNEL.5)
1521 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1523 return LOCAL_Alloc( CURRENT_DS, flags, size );
1527 /***********************************************************************
1528 * WIN16_LocalAlloc
1530 void WINAPI WIN16_LocalAlloc( CONTEXT *context )
1532 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1533 UINT16 flags = (UINT16)stack[3];
1534 WORD size = (WORD)stack[2];
1535 TRACE_(local)("WIN16LocalAlloc: %04x %d \n", flags, size);
1536 AX_reg(context) = CX_reg(context) = LOCAL_Alloc( DS_reg(context), flags, size );
1540 /***********************************************************************
1541 * LocalReAlloc16 (KERNEL.6)
1543 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1545 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1549 /***********************************************************************
1550 * LocalFree16 (KERNEL.7)
1552 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1554 return LOCAL_Free( CURRENT_DS, handle );
1558 /***********************************************************************
1559 * LocalLock16 (KERNEL.8)
1561 * Note: only the offset part of the pointer is returned by the relay code.
1563 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1565 return LOCAL_LockSegptr( CURRENT_DS, handle );
1569 /***********************************************************************
1570 * LocalUnlock16 (KERNEL.9)
1572 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1574 return LOCAL_Unlock( CURRENT_DS, handle );
1578 /***********************************************************************
1579 * LocalSize16 (KERNEL.10)
1581 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1583 return LOCAL_Size( CURRENT_DS, handle );
1587 /***********************************************************************
1588 * LocalHandle16 (KERNEL.11)
1590 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1592 return LOCAL_Handle( CURRENT_DS, addr );
1596 /***********************************************************************
1597 * LocalFlags16 (KERNEL.12)
1599 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1601 return LOCAL_Flags( CURRENT_DS, handle );
1605 /***********************************************************************
1606 * LocalCompact16 (KERNEL.13)
1608 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1610 TRACE_(local)("%04x\n", minfree );
1611 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1615 /***********************************************************************
1616 * LocalNotify (KERNEL.14)
1618 * Installs a callback function that is called for local memory events
1619 * Callback function prototype is
1620 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1621 * wMsg:
1622 * - LN_OUTOFMEM
1623 * NotifyFunc seems to be responsible for allocating some memory,
1624 * returns TRUE for success.
1625 * wArg = number of bytes needed additionally
1626 * - LN_MOVE
1627 * hMem = handle; wArg = old mem location
1628 * - LN_DISCARD
1629 * NotifyFunc seems to be strongly encouraged to return TRUE,
1630 * otherwise LogError() gets called.
1631 * hMem = handle; wArg = flags
1633 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1635 LOCALHEAPINFO *pInfo;
1636 FARPROC16 oldNotify;
1637 HANDLE16 ds = CURRENT_DS;
1639 if (!(pInfo = LOCAL_GetHeap( ds )))
1641 ERR_(local)("(%04x): Local heap not found\n", ds );
1642 LOCAL_PrintHeap( ds );
1643 return 0;
1645 TRACE_(local)("(%04x): %08lx\n", ds, (DWORD)func );
1646 FIXME_(local)("Half implemented\n");
1647 oldNotify = pInfo->notify;
1648 pInfo->notify = func;
1649 return oldNotify;
1653 /***********************************************************************
1654 * LocalShrink16 (KERNEL.121)
1656 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1658 TRACE_(local)("%04x %04x\n", handle, newsize );
1659 return 0;
1663 /***********************************************************************
1664 * GetHeapSpaces (KERNEL.138)
1666 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1668 NE_MODULE *pModule;
1669 WORD ds;
1671 if (!(pModule = NE_GetPtr( module ))) return 0;
1672 ds =
1673 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1674 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1678 /***********************************************************************
1679 * LocalCountFree (KERNEL.161)
1681 WORD WINAPI LocalCountFree16(void)
1683 return LOCAL_CountFree( CURRENT_DS );
1687 /***********************************************************************
1688 * LocalHeapSize (KERNEL.162)
1690 WORD WINAPI LocalHeapSize16(void)
1692 TRACE_(local)("(void)\n" );
1693 return LOCAL_HeapSize( CURRENT_DS );
1697 /***********************************************************************
1698 * LocalHandleDelta (KERNEL.310)
1700 WORD WINAPI LocalHandleDelta16( WORD delta )
1702 LOCALHEAPINFO *pInfo;
1704 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1706 ERR_(local)("Local heap not found\n");
1707 LOCAL_PrintHeap( CURRENT_DS );
1708 return 0;
1710 if (delta) pInfo->hdelta = delta;
1711 TRACE_(local)("returning %04x\n", pInfo->hdelta);
1712 return pInfo->hdelta;
1716 /***********************************************************************
1717 * LocalInfo (TOOLHELP.56)
1719 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1721 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(WIN16_GlobalLock16(handle)));
1722 if (!pInfo) return FALSE;
1723 pLocalInfo->wcItems = pInfo->items;
1724 return TRUE;
1728 /***********************************************************************
1729 * LocalFirst (TOOLHELP.57)
1731 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1733 WORD ds = GlobalHandleToSel16( handle );
1734 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1735 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1736 if (!pInfo) return FALSE;
1738 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1739 pLocalEntry->wAddress = pLocalEntry->hHandle;
1740 pLocalEntry->wFlags = LF_FIXED;
1741 pLocalEntry->wcLock = 0;
1742 pLocalEntry->wType = LT_NORMAL;
1743 pLocalEntry->hHeap = handle;
1744 pLocalEntry->wHeapType = NORMAL_HEAP;
1745 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1746 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1747 return TRUE;
1751 /***********************************************************************
1752 * LocalNext (TOOLHELP.58)
1754 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1756 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1757 char *ptr = PTR_SEG_OFF_TO_LIN( ds, 0 );
1758 LOCALARENA *pArena;
1760 if (!LOCAL_GetHeap( ds )) return FALSE;
1761 if (!pLocalEntry->wNext) return FALSE;
1762 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1764 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1765 pLocalEntry->wAddress = pLocalEntry->hHandle;
1766 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1767 pLocalEntry->wcLock = 0;
1768 pLocalEntry->wType = LT_NORMAL;
1769 if (pArena->next != pLocalEntry->wNext) /* last one? */
1770 pLocalEntry->wNext = pArena->next;
1771 else
1772 pLocalEntry->wNext = 0;
1773 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1774 return TRUE;
1778 /***********************************************************************
1779 * LocalAlloc32 (KERNEL32.371)
1780 * RETURNS
1781 * Handle: Success
1782 * NULL: Failure
1784 HLOCAL WINAPI LocalAlloc(
1785 UINT flags, /* [in] Allocation attributes */
1786 DWORD size /* [in] Number of bytes to allocate */
1788 return (HLOCAL)GlobalAlloc( flags, size );
1792 /***********************************************************************
1793 * LocalCompact32 (KERNEL32.372)
1795 UINT WINAPI LocalCompact( UINT minfree )
1797 return 0; /* LocalCompact does nothing in Win32 */
1801 /***********************************************************************
1802 * LocalFlags32 (KERNEL32.374)
1803 * RETURNS
1804 * Value specifying allocation flags and lock count.
1805 * LMEM_INVALID_HANDLE: Failure
1807 UINT WINAPI LocalFlags(
1808 HLOCAL handle /* [in] Handle of memory object */
1810 return GlobalFlags( (HGLOBAL)handle );
1814 /***********************************************************************
1815 * LocalFree32 (KERNEL32.375)
1816 * RETURNS
1817 * NULL: Success
1818 * Handle: Failure
1820 HLOCAL WINAPI LocalFree(
1821 HLOCAL handle /* [in] Handle of memory object */
1823 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
1827 /***********************************************************************
1828 * LocalHandle32 (KERNEL32.376)
1829 * RETURNS
1830 * Handle: Success
1831 * NULL: Failure
1833 HLOCAL WINAPI LocalHandle(
1834 LPCVOID ptr /* [in] Address of local memory object */
1836 return (HLOCAL)GlobalHandle( ptr );
1840 /***********************************************************************
1841 * LocalLock32 (KERNEL32.377)
1842 * Locks a local memory object and returns pointer to the first byte
1843 * of the memory block.
1845 * RETURNS
1846 * Pointer: Success
1847 * NULL: Failure
1849 LPVOID WINAPI LocalLock(
1850 HLOCAL handle /* [in] Address of local memory object */
1852 return GlobalLock( (HGLOBAL)handle );
1856 /***********************************************************************
1857 * LocalReAlloc32 (KERNEL32.378)
1858 * RETURNS
1859 * Handle: Success
1860 * NULL: Failure
1862 HLOCAL WINAPI LocalReAlloc(
1863 HLOCAL handle, /* [in] Handle of memory object */
1864 DWORD size, /* [in] New size of block */
1865 UINT flags /* [in] How to reallocate object */
1867 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1871 /***********************************************************************
1872 * LocalShrink32 (KERNEL32.379)
1874 UINT WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1876 return 0; /* LocalShrink does nothing in Win32 */
1880 /***********************************************************************
1881 * LocalSize32 (KERNEL32.380)
1882 * RETURNS
1883 * Size: Success
1884 * 0: Failure
1886 UINT WINAPI LocalSize(
1887 HLOCAL handle /* [in] Handle of memory object */
1889 return GlobalSize( (HGLOBAL)handle );
1893 /***********************************************************************
1894 * LocalUnlock32 (KERNEL32.381)
1895 * RETURNS
1896 * TRUE: Object is still locked
1897 * FALSE: Object is unlocked
1899 BOOL WINAPI LocalUnlock(
1900 HLOCAL handle /* [in] Handle of memory object */
1902 return GlobalUnlock( (HGLOBAL)handle );