Fixed a couple of races with exiting threads in suspend_for_ptrace().
[wine/gsoc_dplay.git] / memory / local.c
blob9dbd0f05ca128d3c38c8d27bec06aa58c8df41c6
1 /*
2 * Local heap functions
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Huw Davies
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Note:
24 * All local heap functions need the current DS as first parameter
25 * when called from the emulation library, so they take one more
26 * parameter than usual.
29 #include "config.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include "wine/winbase16.h"
34 #include "instance.h"
35 #include "local.h"
36 #include "module.h"
37 #include "stackframe.h"
38 #include "toolhelp.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(local);
42 WINE_DECLARE_DEBUG_CHANNEL(heap);
44 typedef struct
46 /* Arena header */
47 WORD prev; /* Previous arena | arena type */
48 WORD next; /* Next arena */
49 /* Start of the memory block or free-list info */
50 WORD size; /* Size of the free block */
51 WORD free_prev; /* Previous free block */
52 WORD free_next; /* Next free block */
53 } LOCALARENA;
55 #define ARENA_HEADER_SIZE 4
56 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
58 /* Arena types (stored in 'prev' field of the arena) */
59 #define LOCAL_ARENA_FREE 0
60 #define LOCAL_ARENA_FIXED 1
62 /* LocalNotify() msgs */
64 #define LN_OUTOFMEM 0
65 #define LN_MOVE 1
66 #define LN_DISCARD 2
68 /* Layout of a handle entry table
70 * WORD count of entries
71 * LOCALHANDLEENTRY[count] entries
72 * WORD near ptr to next table
74 typedef struct
76 WORD addr; /* Address of the MOVEABLE block */
77 BYTE flags; /* Flags for this block */
78 BYTE lock; /* Lock count */
79 } LOCALHANDLEENTRY;
82 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
83 * in case something actually relies on this.
84 * Note that if the architecture does not allow unaligned accesses, we make
85 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
87 * An unused handle has lock = flags = 0xff. In windows addr is that of next
88 * free handle, at the moment in wine we set it to 0.
90 * A discarded block's handle has lock = addr = 0 and flags = 0x40
91 * (LMEM_DISCARDED >> 8)
94 #ifdef ALLOW_UNALIGNED_ACCESS
95 #define MOVEABLE_PREFIX sizeof(HLOCAL16)
96 #else
97 #define MOVEABLE_PREFIX sizeof(int)
98 #endif
101 #include "pshpack1.h"
103 typedef struct
105 WORD check; /* 00 Heap checking flag */
106 WORD freeze; /* 02 Heap frozen flag */
107 WORD items; /* 04 Count of items on the heap */
108 WORD first; /* 06 First item of the heap */
109 WORD pad1; /* 08 Always 0 */
110 WORD last; /* 0a Last item of the heap */
111 WORD pad2; /* 0c Always 0 */
112 BYTE ncompact; /* 0e Compactions counter */
113 BYTE dislevel; /* 0f Discard level */
114 DWORD distotal; /* 10 Total bytes discarded */
115 WORD htable; /* 14 Pointer to handle table */
116 WORD hfree; /* 16 Pointer to free handle table */
117 WORD hdelta; /* 18 Delta to expand the handle table */
118 WORD expand; /* 1a Pointer to expand function (unused) */
119 WORD pstat; /* 1c Pointer to status structure (unused) */
120 FARPROC16 notify WINE_PACKED; /* 1e Pointer to LocalNotify() function */
121 WORD lock; /* 22 Lock count for the heap */
122 WORD extra; /* 24 Extra bytes to allocate when expanding */
123 WORD minsize; /* 26 Minimum size of the heap */
124 WORD magic; /* 28 Magic number */
125 } LOCALHEAPINFO;
127 #include "poppack.h"
129 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
131 /* All local heap allocations are aligned on 4-byte boundaries */
132 #define LALIGN(word) (((word) + 3) & ~3)
134 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
135 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
136 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
137 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
139 /* determine whether the handle belongs to a fixed or a moveable block */
140 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
141 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
144 /* ### start build ### */
145 extern WORD CALLBACK LOCAL_CallTo16_word_www(FARPROC16,WORD,HLOCAL16,WORD);
146 /* ### stop build ### */
148 /***********************************************************************
149 * LOCAL_GetHeap
151 * Return a pointer to the local heap, making sure it exists.
153 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
155 LOCALHEAPINFO *pInfo;
156 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
157 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
158 if (!ptr || !ptr->heap) return NULL;
159 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
161 WARN("Bad pointer\n");
162 return NULL;
164 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
165 if (pInfo->magic != LOCAL_HEAP_MAGIC)
167 WARN("Bad magic\n");
168 return NULL;
170 return pInfo;
174 /***********************************************************************
175 * LOCAL_MakeBlockFree
177 * Make a block free, inserting it in the free-list.
178 * 'block' is the handle of the block arena; 'baseptr' points to
179 * the beginning of the data segment containing the heap.
181 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
183 LOCALARENA *pArena, *pNext;
184 WORD next;
186 /* Mark the block as free */
188 pArena = ARENA_PTR( baseptr, block );
189 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
190 pArena->size = pArena->next - block;
192 /* Find the next free block (last block is always free) */
194 next = pArena->next;
195 for (;;)
197 pNext = ARENA_PTR( baseptr, next );
198 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
199 next = pNext->next;
202 TRACE("%04x, next %04x\n", block, next );
203 /* Insert the free block in the free-list */
205 pArena->free_prev = pNext->free_prev;
206 pArena->free_next = next;
207 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
208 pNext->free_prev = block;
212 /***********************************************************************
213 * LOCAL_RemoveFreeBlock
215 * Remove a block from the free-list.
216 * 'block' is the handle of the block arena; 'baseptr' points to
217 * the beginning of the data segment containing the heap.
219 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
221 /* Mark the block as fixed */
223 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
224 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
226 /* Remove it from the list */
228 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
229 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
233 /***********************************************************************
234 * LOCAL_AddBlock
236 * Insert a new block in the heap.
237 * 'new' is the handle of the new block arena; 'baseptr' points to
238 * the beginning of the data segment containing the heap; 'prev' is
239 * the block before the new one.
241 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
243 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
244 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
246 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
247 pNew->next = pPrev->next;
248 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
249 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
250 pPrev->next = new;
254 /***********************************************************************
255 * LOCAL_RemoveBlock
257 * Remove a block from the heap.
258 * 'block' is the handle of the block arena; 'baseptr' points to
259 * the beginning of the data segment containing the heap.
261 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
263 LOCALARENA *pArena, *pTmp;
265 /* Remove the block from the free-list */
267 TRACE("\n");
268 pArena = ARENA_PTR( baseptr, block );
269 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
270 LOCAL_RemoveFreeBlock( baseptr, block );
272 /* If the previous block is free, expand its size */
274 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
275 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
276 pTmp->size += pArena->next - block;
278 /* Remove the block from the linked list */
280 pTmp->next = pArena->next;
281 pTmp = ARENA_PTR( baseptr, pArena->next );
282 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
286 /***********************************************************************
287 * LOCAL_PrintHeap
289 static void LOCAL_PrintHeap( HANDLE16 ds )
291 char *ptr;
292 LOCALHEAPINFO *pInfo;
293 WORD arena;
295 /* FIXME - the test should be done when calling the function!
296 plus is not clear that we should print this info
297 only when TRACE_ON is on! */
298 if(!TRACE_ON(local)) return;
300 ptr = MapSL( MAKESEGPTR( ds, 0 ));
301 pInfo = LOCAL_GetHeap( ds );
303 if (!pInfo)
305 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds );
306 return;
308 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
309 ds, pInfo->first, pInfo->last, pInfo->items );
311 arena = pInfo->first;
312 for (;;)
314 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
315 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena,
316 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
317 if (arena == pInfo->first)
319 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
320 pArena->size, pArena->free_prev, pArena->free_next );
322 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
324 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
325 pArena->size, pArena->free_prev, pArena->free_next );
326 if (pArena->next == arena) break; /* last one */
327 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
329 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
330 break;
333 if (pArena->next == arena)
335 DPRINTF( "*** last block is not marked free\n" );
336 break;
338 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
340 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
341 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
342 break;
344 arena = pArena->next;
349 /***********************************************************************
350 * LocalInit (KERNEL.4)
352 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
354 char *ptr;
355 WORD heapInfoArena, freeArena, lastArena;
356 LOCALHEAPINFO *pHeapInfo;
357 LOCALARENA *pArena, *pFirstArena, *pLastArena;
358 NE_MODULE *pModule;
359 BOOL16 ret = FALSE;
361 /* The initial layout of the heap is: */
362 /* - first arena (FIXED) */
363 /* - heap info structure (FIXED) */
364 /* - large free block (FREE) */
365 /* - last arena (FREE) */
367 TRACE("%04x %04x-%04x\n", selector, start, end);
368 if (!selector) selector = CURRENT_DS;
370 if (TRACE_ON(heap))
372 /* If TRACE_ON(heap) is set, the global heap blocks are */
373 /* cleared before use, so we can test for double initialization. */
374 if (LOCAL_GetHeap(selector))
376 ERR("Heap %04x initialized twice.\n", selector);
377 LOCAL_PrintHeap(selector);
381 if (start == 0)
383 /* start == 0 means: put the local heap at the end of the segment */
385 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
386 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
387 if ( end > 0xfffe ) end = 0xfffe;
388 start -= end;
389 end += start;
391 /* Paranoid check */
393 if ((pModule = NE_GetPtr( GlobalHandle16( selector ) )))
395 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
396 int segNr;
398 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
399 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
400 break;
402 if ( segNr < pModule->seg_count )
404 WORD minsize = pSeg->minsize;
405 if ( pModule->ss == segNr+1 )
406 minsize += pModule->stack_size;
408 TRACE(" new start %04x, minstart: %04x\n", start, minsize);
412 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
414 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
415 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
416 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
417 + sizeof(LOCALHEAPINFO) );
418 lastArena = (end - sizeof(LOCALARENA)) & ~3;
420 /* Make sure there's enough space. */
422 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
424 /* Initialise the first arena */
426 pFirstArena = ARENA_PTR( ptr, start );
427 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
428 pFirstArena->next = heapInfoArena;
429 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
430 pFirstArena->free_prev = start; /* this one */
431 pFirstArena->free_next = freeArena;
433 /* Initialise the arena of the heap info structure */
435 pArena = ARENA_PTR( ptr, heapInfoArena );
436 pArena->prev = start | LOCAL_ARENA_FIXED;
437 pArena->next = freeArena;
439 /* Initialise the heap info structure */
441 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
442 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
443 pHeapInfo->items = 4;
444 pHeapInfo->first = start;
445 pHeapInfo->last = lastArena;
446 pHeapInfo->htable = 0;
447 pHeapInfo->hdelta = 0x20;
448 pHeapInfo->extra = 0x200;
449 pHeapInfo->minsize = lastArena - freeArena;
450 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
452 /* Initialise the large free block */
454 pArena = ARENA_PTR( ptr, freeArena );
455 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
456 pArena->next = lastArena;
457 pArena->size = lastArena - freeArena;
458 pArena->free_prev = start;
459 pArena->free_next = lastArena;
461 /* Initialise the last block */
463 pLastArena = ARENA_PTR( ptr, lastArena );
464 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
465 pLastArena->next = lastArena; /* this one */
466 pLastArena->size = LALIGN(sizeof(LOCALARENA));
467 pLastArena->free_prev = freeArena;
468 pLastArena->free_next = lastArena; /* this one */
470 /* Store the local heap address in the instance data */
472 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
473 LOCAL_PrintHeap( selector );
474 ret = TRUE;
476 done:
477 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
478 return ret;
481 /***********************************************************************
482 * LOCAL_GrowHeap
484 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
486 HANDLE16 hseg;
487 LONG oldsize;
488 LONG end;
489 LOCALHEAPINFO *pHeapInfo;
490 WORD freeArena, lastArena;
491 LOCALARENA *pArena, *pLastArena;
492 char *ptr;
494 hseg = GlobalHandle16( ds );
495 /* maybe mem allocated by Virtual*() ? */
496 if (!hseg) return FALSE;
498 oldsize = GlobalSize16( hseg );
499 /* if nothing can be gained, return */
500 if (oldsize > 0xfff0) return FALSE;
501 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
502 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
503 pHeapInfo = LOCAL_GetHeap( ds );
504 if (pHeapInfo == NULL) {
505 ERR("Heap not found\n" );
506 return FALSE;
508 end = GlobalSize16( hseg );
509 lastArena = (end - sizeof(LOCALARENA)) & ~3;
511 /* Update the HeapInfo */
512 pHeapInfo->items++;
513 freeArena = pHeapInfo->last;
514 pHeapInfo->last = lastArena;
515 pHeapInfo->minsize += end - oldsize;
517 /* grow the old last block */
518 pArena = ARENA_PTR( ptr, freeArena );
519 pArena->size = lastArena - freeArena;
520 pArena->next = lastArena;
521 pArena->free_next = lastArena;
523 /* Initialise the new last block */
525 pLastArena = ARENA_PTR( ptr, lastArena );
526 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
527 pLastArena->next = lastArena; /* this one */
528 pLastArena->size = LALIGN(sizeof(LOCALARENA));
529 pLastArena->free_prev = freeArena;
530 pLastArena->free_next = lastArena; /* this one */
532 /* If block before freeArena is also free then merge them */
533 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
535 LOCAL_RemoveBlock(ptr, freeArena);
536 pHeapInfo->items--;
539 TRACE("Heap expanded\n" );
540 LOCAL_PrintHeap( ds );
541 return TRUE;
545 /***********************************************************************
546 * LOCAL_FreeArena
548 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
550 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
551 LOCALHEAPINFO *pInfo;
552 LOCALARENA *pArena, *pPrev, *pNext;
554 TRACE("%04x ds=%04x\n", arena, ds );
555 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
557 pArena = ARENA_PTR( ptr, arena );
558 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
560 /* shouldn't happen */
561 ERR("Trying to free block %04x twice!\n",
562 arena );
563 LOCAL_PrintHeap( ds );
564 return arena;
567 /* Check if we can merge with the previous block */
569 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
570 pNext = ARENA_PTR( ptr, pArena->next );
571 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
573 arena = pArena->prev & ~3;
574 pArena = pPrev;
575 LOCAL_RemoveBlock( ptr, pPrev->next );
576 pInfo->items--;
578 else /* Make a new free block */
580 LOCAL_MakeBlockFree( ptr, arena );
583 /* Check if we can merge with the next block */
585 if ((pArena->next == pArena->free_next) &&
586 (pArena->next != pInfo->last))
588 LOCAL_RemoveBlock( ptr, pArena->next );
589 pInfo->items--;
591 return 0;
595 /***********************************************************************
596 * LOCAL_ShrinkArena
598 * Shrink an arena by creating a free block at its end if possible.
599 * 'size' includes the arena header, and must be aligned.
601 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
603 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
604 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
606 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
608 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
609 if (!pInfo) return;
610 LOCAL_AddBlock( ptr, arena, arena + size );
611 pInfo->items++;
612 LOCAL_FreeArena( ds, arena + size );
617 /***********************************************************************
618 * LOCAL_GrowArenaDownward
620 * Grow an arena downward by using the previous arena (must be free).
622 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
624 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
625 LOCALHEAPINFO *pInfo;
626 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
627 WORD prevArena = pArena->prev & ~3;
628 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
629 WORD offset, size;
630 char *p;
632 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
633 offset = pPrevArena->size;
634 size = pArena->next - arena - ARENA_HEADER_SIZE;
635 LOCAL_RemoveFreeBlock( ptr, prevArena );
636 LOCAL_RemoveBlock( ptr, arena );
637 pInfo->items--;
638 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
639 while (offset < size)
641 memcpy( p, p + offset, offset );
642 p += offset;
643 size -= offset;
645 if (size) memcpy( p, p + offset, size );
646 LOCAL_ShrinkArena( ds, prevArena, newsize );
651 /***********************************************************************
652 * LOCAL_GrowArenaUpward
654 * Grow an arena upward by using the next arena (must be free and big
655 * enough). Newsize includes the arena header and must be aligned.
657 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
659 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
660 LOCALHEAPINFO *pInfo;
661 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
662 WORD nextArena = pArena->next;
664 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
665 LOCAL_RemoveBlock( ptr, nextArena );
666 pInfo->items--;
667 LOCAL_ShrinkArena( ds, arena, newsize );
671 /***********************************************************************
672 * LOCAL_GetFreeSpace
674 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
676 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
677 LOCALHEAPINFO *pInfo;
678 LOCALARENA *pArena;
679 WORD arena;
680 WORD freespace = 0;
682 if (!(pInfo = LOCAL_GetHeap( ds )))
684 ERR("Local heap not found\n" );
685 LOCAL_PrintHeap(ds);
686 return 0;
688 arena = pInfo->first;
689 pArena = ARENA_PTR( ptr, arena );
690 while (arena != pArena->free_next)
692 arena = pArena->free_next;
693 pArena = ARENA_PTR( ptr, arena );
694 if (pArena->size >= freespace) freespace = pArena->size;
696 /* FIXME doesn't yet calculate space that would become free if everything
697 were discarded when countdiscard == 1 */
698 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
699 else freespace -= ARENA_HEADER_SIZE;
700 return freespace;
704 /***********************************************************************
705 * LOCAL_Compact
707 WORD LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
709 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
710 LOCALHEAPINFO *pInfo;
711 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
712 WORD arena, movearena, finalarena, table;
713 WORD count, movesize, size;
714 WORD freespace;
715 LOCALHANDLEENTRY *pEntry;
717 if (!(pInfo = LOCAL_GetHeap( ds )))
719 ERR("Local heap not found\n" );
720 LOCAL_PrintHeap(ds);
721 return 0;
723 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
724 ds, minfree, flags);
725 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
726 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
728 TRACE("Returning %04x.\n", freespace);
729 return freespace;
731 TRACE("Compacting heap %04x.\n", ds);
732 table = pInfo->htable;
733 while(table)
735 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
736 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
738 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
740 /* OK we can move this one if we want */
741 TRACE("handle %04x (block %04x) can be moved.\n",
742 (WORD)((char *)pEntry - ptr), pEntry->addr);
743 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
744 pMoveArena = ARENA_PTR(ptr, movearena);
745 movesize = pMoveArena->next - movearena;
746 arena = pInfo->first;
747 pArena = ARENA_PTR(ptr, arena);
748 size = 0xffff;
749 finalarena = 0;
750 /* Try to find the smallest arena that will do, */
751 /* which is below us in memory */
752 for(;;)
754 arena = pArena->free_next;
755 pArena = ARENA_PTR(ptr, arena);
756 if(arena >= movearena)
757 break;
758 if(arena == pArena->free_next)
759 break;
760 if((pArena->size >= movesize) && (pArena->size < size))
762 size = pArena->size;
763 finalarena = arena;
766 if (finalarena) /* Actually got somewhere to move */
768 TRACE("Moving it to %04x.\n", finalarena);
769 pFinalArena = ARENA_PTR(ptr, finalarena);
770 size = pFinalArena->size;
771 LOCAL_RemoveFreeBlock(ptr, finalarena);
772 LOCAL_ShrinkArena( ds, finalarena, movesize );
773 /* Copy the arena to it's new location */
774 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
775 (char *)pMoveArena + ARENA_HEADER_SIZE,
776 movesize - ARENA_HEADER_SIZE );
777 /* Free the old location */
778 LOCAL_FreeArena(ds, movearena);
779 if (pInfo->notify)
780 LOCAL_CallTo16_word_www(pInfo->notify, LN_MOVE,
781 (WORD)((char *)pEntry - ptr), pEntry->addr);
782 /* Update handle table entry */
783 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
785 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
786 == LOCAL_ARENA_FREE)
788 /* Previous arena is free (but < movesize) */
789 /* so we can 'slide' movearena down into it */
790 finalarena = pMoveArena->prev & ~3;
791 LOCAL_GrowArenaDownward( ds, movearena, movesize );
792 /* Update handle table entry */
793 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
797 table = *(WORD *)pEntry;
799 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
800 if(freespace >= minfree || (flags & LMEM_NODISCARD))
802 TRACE("Returning %04x.\n", freespace);
803 return freespace;
806 table = pInfo->htable;
807 while(table)
809 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
810 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
812 if(pEntry->addr && pEntry->lock == 0 &&
813 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
815 TRACE("Discarding handle %04x (block %04x).\n",
816 (char *)pEntry - ptr, pEntry->addr);
817 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
818 if (pInfo->notify)
819 LOCAL_CallTo16_word_www(pInfo->notify, LN_DISCARD,
820 (char *)pEntry - ptr, pEntry->flags);
821 pEntry->addr = 0;
822 pEntry->flags = (LMEM_DISCARDED >> 8);
825 table = *(WORD *)pEntry;
827 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
831 /***********************************************************************
832 * LOCAL_FindFreeBlock
834 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
836 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
837 LOCALHEAPINFO *pInfo;
838 LOCALARENA *pArena;
839 WORD arena;
841 if (!(pInfo = LOCAL_GetHeap( ds )))
843 ERR("Local heap not found\n" );
844 LOCAL_PrintHeap(ds);
845 return 0;
848 arena = pInfo->first;
849 pArena = ARENA_PTR( ptr, arena );
850 for (;;) {
851 arena = pArena->free_next;
852 pArena = ARENA_PTR( ptr, arena );
853 if (arena == pArena->free_next) break;
854 if (pArena->size >= size) return arena;
856 TRACE("not enough space\n" );
857 LOCAL_PrintHeap(ds);
858 return 0;
862 /***********************************************************************
863 * get_heap_name
865 static const char *get_heap_name( WORD ds )
867 HINSTANCE16 inst = LoadLibrary16( "GDI" );
868 if (ds == GlobalHandleToSel16( inst ))
870 FreeLibrary16( inst );
871 return "GDI";
873 FreeLibrary16( inst );
874 inst = LoadLibrary16( "USER" );
875 if (ds == GlobalHandleToSel16( inst ))
877 FreeLibrary16( inst );
878 return "USER";
880 FreeLibrary16( inst );
881 return "local";
884 /***********************************************************************
885 * LOCAL_GetBlock
886 * The segment may get moved around in this function, so all callers
887 * should reset their pointer variables.
889 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
891 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
892 LOCALHEAPINFO *pInfo;
893 LOCALARENA *pArena;
894 WORD arena;
896 if (!(pInfo = LOCAL_GetHeap( ds )))
898 ERR("Local heap not found\n");
899 LOCAL_PrintHeap(ds);
900 return 0;
903 size += ARENA_HEADER_SIZE;
904 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
906 #if 0
907 notify_done:
908 #endif
909 /* Find a suitable free block */
910 arena = LOCAL_FindFreeBlock( ds, size );
911 if (arena == 0) {
912 /* no space: try to make some */
913 LOCAL_Compact( ds, size, flags );
914 arena = LOCAL_FindFreeBlock( ds, size );
916 if (arena == 0) {
917 /* still no space: try to grow the segment */
918 if (!(LOCAL_GrowHeap( ds )))
920 #if 0
921 /* FIXME: doesn't work correctly yet */
922 if ((pInfo->notify) && (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds - 20, size))) /* FIXME: "size" correct ? (should indicate bytes needed) */
923 goto notify_done;
924 #endif
925 ERR( "not enough space in %s heap %04x for %d bytes\n",
926 get_heap_name(ds), ds, size );
927 return 0;
929 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
930 pInfo = LOCAL_GetHeap( ds );
931 arena = LOCAL_FindFreeBlock( ds, size );
933 if (arena == 0) {
934 ERR( "not enough space in %s heap %04x for %d bytes\n",
935 get_heap_name(ds), ds, size );
936 #if 0
937 if ((pInfo->notify) &&
938 /* FIXME: "size" correct ? (should indicate bytes needed) */
939 (LOCAL_CallTo16_word_www(pInfo->notify, LN_OUTOFMEM, ds, size)))
940 goto notify_done;
941 #endif
942 return 0;
945 /* Make a block out of the free arena */
946 pArena = ARENA_PTR( ptr, arena );
947 TRACE("size = %04x, arena %04x size %04x\n",
948 size, arena, pArena->size );
949 LOCAL_RemoveFreeBlock( ptr, arena );
950 LOCAL_ShrinkArena( ds, arena, size );
952 if (flags & LMEM_ZEROINIT)
953 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
954 return arena + ARENA_HEADER_SIZE;
958 /***********************************************************************
959 * LOCAL_NewHTable
961 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
963 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
964 LOCALHEAPINFO *pInfo;
965 LOCALHANDLEENTRY *pEntry;
966 HLOCAL16 handle;
967 int i;
969 TRACE("\n" );
970 if (!(pInfo = LOCAL_GetHeap( ds )))
972 ERR("Local heap not found\n");
973 LOCAL_PrintHeap(ds);
974 return FALSE;
977 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
978 + 2 * sizeof(WORD), LMEM_FIXED )))
979 return FALSE;
980 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
981 ERR("ptr == NULL after GetBlock.\n");
982 if (!(pInfo = LOCAL_GetHeap( ds )))
983 ERR("pInfo == NULL after GetBlock.\n");
985 /* Fill the entry table */
987 *(WORD *)(ptr + handle) = pInfo->hdelta;
988 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
989 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
990 pEntry->lock = pEntry->flags = 0xff;
991 pEntry->addr = 0;
993 *(WORD *)pEntry = pInfo->htable;
994 pInfo->htable = handle;
995 return TRUE;
999 /***********************************************************************
1000 * LOCAL_GetNewHandleEntry
1002 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
1004 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1005 LOCALHEAPINFO *pInfo;
1006 LOCALHANDLEENTRY *pEntry = NULL;
1007 WORD table;
1009 if (!(pInfo = LOCAL_GetHeap( ds )))
1011 ERR("Local heap not found\n");
1012 LOCAL_PrintHeap(ds);
1013 return 0;
1016 /* Find a free slot in existing tables */
1018 table = pInfo->htable;
1019 while (table)
1021 WORD count = *(WORD *)(ptr + table);
1022 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1023 for (; count > 0; count--, pEntry++)
1024 if (pEntry->lock == 0xff) break;
1025 if (count) break;
1026 table = *(WORD *)pEntry;
1029 if (!table) /* We need to create a new table */
1031 if (!LOCAL_NewHTable( ds )) return 0;
1032 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1033 pInfo = LOCAL_GetHeap( ds );
1034 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1037 /* Now allocate this entry */
1039 pEntry->lock = 0;
1040 pEntry->flags = 0;
1041 TRACE("(%04x): %04x\n",
1042 ds, ((char *)pEntry - ptr) );
1043 return (HLOCAL16)((char *)pEntry - ptr);
1047 /***********************************************************************
1048 * LOCAL_FreeHandleEntry
1050 * Free a handle table entry.
1052 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1054 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1055 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1056 LOCALHEAPINFO *pInfo;
1057 WORD *pTable;
1058 WORD table, count, i;
1060 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1062 /* Find the table where this handle comes from */
1064 pTable = &pInfo->htable;
1065 while (*pTable)
1067 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1068 if ((handle >= *pTable + sizeof(WORD)) &&
1069 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1070 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1072 if (!*pTable)
1074 ERR("Invalid entry %04x\n", handle);
1075 LOCAL_PrintHeap( ds );
1076 return;
1079 /* Make the entry free */
1081 pEntry->addr = 0; /* just in case */
1082 pEntry->lock = 0xff;
1083 pEntry->flags = 0xff;
1084 /* Now check if all entries in this table are free */
1086 table = *pTable;
1087 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1088 count = *(WORD *)(ptr + table);
1089 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1091 /* Remove the table from the linked list and free it */
1093 TRACE("(%04x): freeing table %04x\n",
1094 ds, table);
1095 *pTable = *(WORD *)pEntry;
1096 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1100 /***********************************************************************
1101 * LOCAL_Free
1103 * Implementation of LocalFree().
1105 HLOCAL16 LOCAL_Free( HANDLE16 ds, HLOCAL16 handle )
1107 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1109 TRACE("%04x ds=%04x\n", handle, ds );
1111 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1112 if (HANDLE_FIXED( handle ))
1114 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1115 else return handle; /* couldn't free it */
1117 else
1119 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1120 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1122 TRACE("real block at %04x\n",
1123 pEntry->addr );
1124 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1125 return handle; /* couldn't free it */
1127 LOCAL_FreeHandleEntry( ds, handle );
1128 return 0; /* OK */
1133 /***********************************************************************
1134 * LOCAL_Alloc
1136 * Implementation of LocalAlloc().
1139 HLOCAL16 LOCAL_Alloc( HANDLE16 ds, WORD flags, WORD size )
1141 char *ptr;
1142 HLOCAL16 handle;
1144 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1146 if(size > 0 && size <= 4) size = 5;
1147 if (flags & LMEM_MOVEABLE)
1149 LOCALHANDLEENTRY *plhe;
1150 HLOCAL16 hmem;
1152 if(size)
1154 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1155 return 0;
1157 else /* We just need to allocate a discarded handle */
1158 hmem = 0;
1159 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1161 WARN("Couldn't get handle.\n");
1162 if(hmem)
1163 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1164 return 0;
1166 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1167 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1168 plhe->lock = 0;
1169 if(hmem)
1171 plhe->addr = hmem + MOVEABLE_PREFIX;
1172 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1173 *(HLOCAL16 *)(ptr + hmem) = handle;
1175 else
1177 plhe->addr = 0;
1178 plhe->flags = LMEM_DISCARDED >> 8;
1181 else /* FIXED */
1183 if(!size)
1184 return 0;
1185 handle = LOCAL_GetBlock( ds, size, flags );
1187 return handle;
1191 /***********************************************************************
1192 * LOCAL_ReAlloc
1194 * Implementation of LocalReAlloc().
1196 HLOCAL16 LOCAL_ReAlloc( HANDLE16 ds, HLOCAL16 handle, WORD size, WORD flags )
1198 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1199 LOCALHEAPINFO *pInfo;
1200 LOCALARENA *pArena, *pNext;
1201 LOCALHANDLEENTRY *pEntry = NULL;
1202 WORD arena, oldsize;
1203 HLOCAL16 hmem, blockhandle;
1204 LONG nextarena;
1206 if (!handle) return 0;
1207 if(HANDLE_MOVEABLE(handle) &&
1208 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1209 return 0;
1211 TRACE("%04x %d %04x ds=%04x\n",
1212 handle, size, flags, ds );
1213 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1215 if (HANDLE_FIXED( handle ))
1216 blockhandle = handle;
1217 else
1219 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1220 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1222 HLOCAL16 hl;
1223 if(pEntry->addr)
1224 WARN("Dicarded block has non-zero addr.\n");
1225 TRACE("ReAllocating discarded block\n");
1226 if(size <= 4) size = 5;
1227 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1228 return 0;
1229 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1230 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1231 pEntry->addr = hl + MOVEABLE_PREFIX;
1232 pEntry->flags = 0;
1233 pEntry->lock = 0;
1234 *(HLOCAL16 *)(ptr + hl) = handle;
1235 return handle;
1237 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1239 ERR("(%04x,%04x): invalid handle\n",
1240 ds, handle );
1241 return 0;
1243 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1244 ERR("Back ptr to handle is invalid\n");
1245 return 0;
1249 if (flags & LMEM_MODIFY)
1251 if (HANDLE_MOVEABLE(handle))
1253 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1254 pEntry->flags = (flags & 0x0f00) >> 8;
1255 TRACE("Changing flags to %x.\n", pEntry->flags);
1257 return handle;
1260 if (!size)
1262 if (flags & LMEM_MOVEABLE)
1264 if (HANDLE_FIXED(handle))
1266 TRACE("Freeing fixed block.\n");
1267 return LOCAL_Free( ds, handle );
1269 else /* Moveable block */
1271 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1272 if (pEntry->lock == 0)
1274 /* discards moveable blocks */
1275 TRACE("Discarding block\n");
1276 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1277 pEntry->addr = 0;
1278 pEntry->flags = (LMEM_DISCARDED >> 8);
1279 return handle;
1282 return 0;
1284 else if(flags == 0)
1286 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1287 if (pEntry->lock == 0)
1289 /* Frees block */
1290 return LOCAL_Free( ds, handle );
1293 return 0;
1296 arena = ARENA_HEADER( blockhandle );
1297 TRACE("arena is %04x\n", arena );
1298 pArena = ARENA_PTR( ptr, arena );
1300 if(size <= 4) size = 5;
1301 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1302 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1303 nextarena = LALIGN(blockhandle + size);
1305 /* Check for size reduction */
1307 if (nextarena <= pArena->next)
1309 TRACE("size reduction, making new free block\n");
1310 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1311 TRACE("returning %04x\n", handle );
1312 return handle;
1315 /* Check if the next block is free and large enough */
1317 pNext = ARENA_PTR( ptr, pArena->next );
1318 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1319 (nextarena <= pNext->next))
1321 TRACE("size increase, making new free block\n");
1322 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1323 TRACE("returning %04x\n", handle );
1324 return handle;
1327 /* Now we have to allocate a new block, but not if (fixed block or locked
1328 block) and no LMEM_MOVEABLE */
1330 if (!(flags & LMEM_MOVEABLE))
1332 if (HANDLE_FIXED(handle))
1334 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1335 return 0;
1337 else
1339 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1341 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1342 return 0;
1347 hmem = LOCAL_GetBlock( ds, size, flags );
1348 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1349 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1350 { /* a compaction, which might in turn have */
1351 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1352 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1354 if (!hmem)
1356 /* Remove the block from the heap and try again */
1357 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1358 if (!buffer) return 0;
1359 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1360 LOCAL_FreeArena( ds, arena );
1361 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1363 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1365 ERR("Can't restore saved block\n" );
1366 HeapFree( GetProcessHeap(), 0, buffer );
1367 return 0;
1369 size = oldsize;
1371 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1372 memcpy( ptr + hmem, buffer, oldsize );
1373 HeapFree( GetProcessHeap(), 0, buffer );
1375 else
1377 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1378 LOCAL_FreeArena( ds, arena );
1380 if (HANDLE_MOVEABLE( handle ))
1382 TRACE("fixing handle\n");
1383 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1384 pEntry->addr = hmem + MOVEABLE_PREFIX;
1385 /* Back ptr should still be correct */
1386 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1387 ERR("back ptr is invalid.\n");
1388 hmem = handle;
1390 if (size == oldsize) hmem = 0; /* Realloc failed */
1391 TRACE("returning %04x\n", hmem );
1392 return hmem;
1396 /***********************************************************************
1397 * LOCAL_InternalLock
1399 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1401 HLOCAL16 old_handle = handle;
1403 if (HANDLE_MOVEABLE(handle))
1405 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1406 if (pEntry->flags == LMEM_DISCARDED) return 0;
1407 if (pEntry->lock < 0xfe) pEntry->lock++;
1408 handle = pEntry->addr;
1410 TRACE("%04x returning %04x\n",
1411 old_handle, handle );
1412 return handle;
1416 /***********************************************************************
1417 * LOCAL_Lock
1419 void *LOCAL_Lock( HANDLE16 ds, HLOCAL16 handle )
1421 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1422 return handle ? ptr + LOCAL_InternalLock( ptr, handle ) : NULL;
1426 /***********************************************************************
1427 * LOCAL_Unlock
1429 BOOL16 LOCAL_Unlock( HANDLE16 ds, HLOCAL16 handle )
1431 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1433 TRACE("%04x\n", handle );
1434 if (HANDLE_MOVEABLE(handle))
1436 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1437 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1438 /* For moveable block, return the new lock count */
1439 /* (see _Windows_Internals_ p. 197) */
1440 return --pEntry->lock;
1442 else return FALSE;
1446 /***********************************************************************
1447 * LOCAL_Size
1449 * Implementation of LocalSize().
1451 WORD LOCAL_Size( HANDLE16 ds, HLOCAL16 handle )
1453 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1454 LOCALARENA *pArena;
1456 TRACE("%04x ds=%04x\n", handle, ds );
1458 if (!handle) return 0;
1459 if (HANDLE_MOVEABLE( handle ))
1461 handle = *(WORD *)(ptr + handle);
1462 if (!handle) return 0;
1463 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1465 else
1466 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1468 return pArena->next - handle;
1472 /***********************************************************************
1473 * LOCAL_Flags
1475 * Implementation of LocalFlags().
1477 WORD LOCAL_Flags( HANDLE16 ds, HLOCAL16 handle )
1479 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1481 if (HANDLE_MOVEABLE(handle))
1483 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1484 TRACE("(%04x,%04x): returning %04x\n",
1485 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1486 return pEntry->lock | (pEntry->flags << 8);
1488 else
1490 TRACE("(%04x,%04x): returning 0\n",
1491 ds, handle );
1492 return 0;
1497 /***********************************************************************
1498 * LOCAL_HeapSize
1500 * Implementation of LocalHeapSize().
1502 WORD LOCAL_HeapSize( HANDLE16 ds )
1504 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1505 if (!pInfo) return 0;
1506 return pInfo->last - pInfo->first;
1510 /***********************************************************************
1511 * LOCAL_CountFree
1513 * Implementation of LocalCountFree().
1515 WORD LOCAL_CountFree( HANDLE16 ds )
1517 WORD arena, total;
1518 LOCALARENA *pArena;
1519 LOCALHEAPINFO *pInfo;
1520 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1522 if (!(pInfo = LOCAL_GetHeap( ds )))
1524 ERR("(%04x): Local heap not found\n", ds );
1525 LOCAL_PrintHeap( ds );
1526 return 0;
1529 total = 0;
1530 arena = pInfo->first;
1531 pArena = ARENA_PTR( ptr, arena );
1532 for (;;)
1534 arena = pArena->free_next;
1535 pArena = ARENA_PTR( ptr, arena );
1536 if (arena == pArena->free_next) break;
1537 total += pArena->size;
1539 TRACE("(%04x): returning %d\n", ds, total);
1540 return total;
1544 /***********************************************************************
1545 * LOCAL_Handle
1547 * Implementation of LocalHandle().
1549 HLOCAL16 LOCAL_Handle( HANDLE16 ds, WORD addr )
1551 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1552 LOCALHEAPINFO *pInfo;
1553 WORD table;
1555 if (!(pInfo = LOCAL_GetHeap( ds )))
1557 ERR("(%04x): Local heap not found\n", ds );
1558 LOCAL_PrintHeap( ds );
1559 return 0;
1562 /* Find the address in the entry tables */
1564 table = pInfo->htable;
1565 while (table)
1567 WORD count = *(WORD *)(ptr + table);
1568 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1569 for (; count > 0; count--, pEntry++)
1570 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1571 table = *(WORD *)pEntry;
1574 return (HLOCAL16)addr; /* Fixed block handle is addr */
1578 /***********************************************************************
1579 * LocalAlloc (KERNEL.5)
1581 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1583 HLOCAL16 ret = LOCAL_Alloc( CURRENT_DS, flags, size );
1584 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
1585 return ret;
1589 /***********************************************************************
1590 * LocalReAlloc (KERNEL.6)
1592 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1594 return LOCAL_ReAlloc( CURRENT_DS, handle, size, flags );
1598 /***********************************************************************
1599 * LocalFree (KERNEL.7)
1601 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1603 return LOCAL_Free( CURRENT_DS, handle );
1607 /***********************************************************************
1608 * LocalLock (KERNEL.8)
1610 * Note: only the offset part of the pointer is returned by the relay code.
1612 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1614 WORD ds = CURRENT_DS;
1615 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1616 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1620 /***********************************************************************
1621 * LocalUnlock (KERNEL.9)
1623 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1625 return LOCAL_Unlock( CURRENT_DS, handle );
1629 /***********************************************************************
1630 * LocalSize (KERNEL.10)
1632 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1634 return LOCAL_Size( CURRENT_DS, handle );
1638 /***********************************************************************
1639 * LocalHandle (KERNEL.11)
1641 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1643 return LOCAL_Handle( CURRENT_DS, addr );
1647 /***********************************************************************
1648 * LocalFlags (KERNEL.12)
1650 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1652 return LOCAL_Flags( CURRENT_DS, handle );
1656 /***********************************************************************
1657 * LocalCompact (KERNEL.13)
1659 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1661 TRACE("%04x\n", minfree );
1662 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1666 /***********************************************************************
1667 * LocalNotify (KERNEL.14)
1669 * Installs a callback function that is called for local memory events
1670 * Callback function prototype is
1671 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1672 * wMsg:
1673 * - LN_OUTOFMEM
1674 * NotifyFunc seems to be responsible for allocating some memory,
1675 * returns TRUE for success.
1676 * wArg = number of bytes needed additionally
1677 * - LN_MOVE
1678 * hMem = handle; wArg = old mem location
1679 * - LN_DISCARD
1680 * NotifyFunc seems to be strongly encouraged to return TRUE,
1681 * otherwise LogError() gets called.
1682 * hMem = handle; wArg = flags
1684 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1686 LOCALHEAPINFO *pInfo;
1687 FARPROC16 oldNotify;
1688 HANDLE16 ds = CURRENT_DS;
1690 if (!(pInfo = LOCAL_GetHeap( ds )))
1692 ERR("(%04x): Local heap not found\n", ds );
1693 LOCAL_PrintHeap( ds );
1694 return 0;
1696 TRACE("(%04x): %08lx\n", ds, (DWORD)func );
1697 FIXME("Half implemented\n");
1698 oldNotify = pInfo->notify;
1699 pInfo->notify = func;
1700 return oldNotify;
1704 /***********************************************************************
1705 * LocalShrink (KERNEL.121)
1707 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1709 TRACE("%04x %04x\n", handle, newsize );
1710 return 0;
1714 /***********************************************************************
1715 * GetHeapSpaces (KERNEL.138)
1717 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1719 NE_MODULE *pModule;
1720 WORD ds;
1722 if (!(pModule = NE_GetPtr( module ))) return 0;
1723 ds =
1724 GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg);
1725 return MAKELONG( LOCAL_CountFree( ds ), LOCAL_HeapSize( ds ) );
1729 /***********************************************************************
1730 * LocalCountFree (KERNEL.161)
1732 WORD WINAPI LocalCountFree16(void)
1734 return LOCAL_CountFree( CURRENT_DS );
1738 /***********************************************************************
1739 * LocalHeapSize (KERNEL.162)
1741 WORD WINAPI LocalHeapSize16(void)
1743 TRACE("(void)\n" );
1744 return LOCAL_HeapSize( CURRENT_DS );
1748 /***********************************************************************
1749 * LocalHandleDelta (KERNEL.310)
1751 WORD WINAPI LocalHandleDelta16( WORD delta )
1753 LOCALHEAPINFO *pInfo;
1755 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1757 ERR("Local heap not found\n");
1758 LOCAL_PrintHeap( CURRENT_DS );
1759 return 0;
1761 if (delta) pInfo->hdelta = delta;
1762 TRACE("returning %04x\n", pInfo->hdelta);
1763 return pInfo->hdelta;
1767 /***********************************************************************
1768 * LocalInfo (TOOLHELP.56)
1770 BOOL16 WINAPI LocalInfo16( LOCALINFO *pLocalInfo, HGLOBAL16 handle )
1772 LOCALHEAPINFO *pInfo = LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle)));
1773 if (!pInfo) return FALSE;
1774 pLocalInfo->wcItems = pInfo->items;
1775 return TRUE;
1779 /***********************************************************************
1780 * LocalFirst (TOOLHELP.57)
1782 BOOL16 WINAPI LocalFirst16( LOCALENTRY *pLocalEntry, HGLOBAL16 handle )
1784 WORD ds = GlobalHandleToSel16( handle );
1785 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1786 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1787 if (!pInfo) return FALSE;
1789 pLocalEntry->hHandle = pInfo->first + ARENA_HEADER_SIZE;
1790 pLocalEntry->wAddress = pLocalEntry->hHandle;
1791 pLocalEntry->wFlags = LF_FIXED;
1792 pLocalEntry->wcLock = 0;
1793 pLocalEntry->wType = LT_NORMAL;
1794 pLocalEntry->hHeap = handle;
1795 pLocalEntry->wHeapType = NORMAL_HEAP;
1796 pLocalEntry->wNext = ARENA_PTR(ptr,pInfo->first)->next;
1797 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1798 return TRUE;
1802 /***********************************************************************
1803 * LocalNext (TOOLHELP.58)
1805 BOOL16 WINAPI LocalNext16( LOCALENTRY *pLocalEntry )
1807 WORD ds = GlobalHandleToSel16( pLocalEntry->hHeap );
1808 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1809 LOCALARENA *pArena;
1811 if (!LOCAL_GetHeap( ds )) return FALSE;
1812 if (!pLocalEntry->wNext) return FALSE;
1813 pArena = ARENA_PTR( ptr, pLocalEntry->wNext );
1815 pLocalEntry->hHandle = pLocalEntry->wNext + ARENA_HEADER_SIZE;
1816 pLocalEntry->wAddress = pLocalEntry->hHandle;
1817 pLocalEntry->wFlags = (pArena->prev & 3) + 1;
1818 pLocalEntry->wcLock = 0;
1819 pLocalEntry->wType = LT_NORMAL;
1820 if (pArena->next != pLocalEntry->wNext) /* last one? */
1821 pLocalEntry->wNext = pArena->next;
1822 else
1823 pLocalEntry->wNext = 0;
1824 pLocalEntry->wSize = pLocalEntry->wNext - pLocalEntry->hHandle;
1825 return TRUE;
1829 /***********************************************************************
1830 * LocalAlloc (KERNEL32.@)
1831 * RETURNS
1832 * Handle: Success
1833 * NULL: Failure
1835 HLOCAL WINAPI LocalAlloc(
1836 UINT flags, /* [in] Allocation attributes */
1837 SIZE_T size /* [in] Number of bytes to allocate */
1839 return (HLOCAL)GlobalAlloc( flags, size );
1843 /***********************************************************************
1844 * LocalCompact (KERNEL32.@)
1846 SIZE_T WINAPI LocalCompact( UINT minfree )
1848 return 0; /* LocalCompact does nothing in Win32 */
1852 /***********************************************************************
1853 * LocalFlags (KERNEL32.@)
1854 * RETURNS
1855 * Value specifying allocation flags and lock count.
1856 * LMEM_INVALID_HANDLE: Failure
1858 UINT WINAPI LocalFlags(
1859 HLOCAL handle /* [in] Handle of memory object */
1861 return GlobalFlags( (HGLOBAL)handle );
1865 /***********************************************************************
1866 * LocalFree (KERNEL32.@)
1867 * RETURNS
1868 * NULL: Success
1869 * Handle: Failure
1871 HLOCAL WINAPI LocalFree(
1872 HLOCAL handle /* [in] Handle of memory object */
1874 return (HLOCAL)GlobalFree( (HGLOBAL)handle );
1878 /***********************************************************************
1879 * LocalHandle (KERNEL32.@)
1880 * RETURNS
1881 * Handle: Success
1882 * NULL: Failure
1884 HLOCAL WINAPI LocalHandle(
1885 LPCVOID ptr /* [in] Address of local memory object */
1887 return (HLOCAL)GlobalHandle( ptr );
1891 /***********************************************************************
1892 * LocalLock (KERNEL32.@)
1893 * Locks a local memory object and returns pointer to the first byte
1894 * of the memory block.
1896 * RETURNS
1897 * Pointer: Success
1898 * NULL: Failure
1900 LPVOID WINAPI LocalLock(
1901 HLOCAL handle /* [in] Address of local memory object */
1903 return GlobalLock( (HGLOBAL)handle );
1907 /***********************************************************************
1908 * LocalReAlloc (KERNEL32.@)
1909 * RETURNS
1910 * Handle: Success
1911 * NULL: Failure
1913 HLOCAL WINAPI LocalReAlloc(
1914 HLOCAL handle, /* [in] Handle of memory object */
1915 SIZE_T size, /* [in] New size of block */
1916 UINT flags /* [in] How to reallocate object */
1918 return (HLOCAL)GlobalReAlloc( (HGLOBAL)handle, size, flags );
1922 /***********************************************************************
1923 * LocalShrink (KERNEL32.@)
1925 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
1927 return 0; /* LocalShrink does nothing in Win32 */
1931 /***********************************************************************
1932 * LocalSize (KERNEL32.@)
1933 * RETURNS
1934 * Size: Success
1935 * 0: Failure
1937 SIZE_T WINAPI LocalSize(
1938 HLOCAL handle /* [in] Handle of memory object */
1940 return GlobalSize( (HGLOBAL)handle );
1944 /***********************************************************************
1945 * LocalUnlock (KERNEL32.@)
1946 * RETURNS
1947 * TRUE: Object is still locked
1948 * FALSE: Object is unlocked
1950 BOOL WINAPI LocalUnlock(
1951 HLOCAL handle /* [in] Handle of memory object */
1953 return GlobalUnlock( (HGLOBAL)handle );