-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathlocal.c
2297 lines (1974 loc) · 67.4 KB
/
local.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 16-bit local heap functions
*
* Copyright 1995 Alexandre Julliard
* Copyright 1996 Huw Davies
* Copyright 1998 Ulrich Weigand
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Note:
* All local heap functions need the current DS as first parameter
* when called from the emulation library, so they take one more
* parameter than usual.
*/
#include "config.h"
#define NONAMELESSUNION
#include <stdlib.h>
#include <string.h>
#include "wine/winbase16.h"
#include "windows/wownt32.h"
#include "winternl.h"
#include "kernel16_private.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(local);
typedef struct
{
/* Arena header */
WORD prev; /* Previous arena | arena type */
WORD next; /* Next arena */
/* Start of the memory block or free-list info */
WORD size; /* Size of the free block */
WORD free_prev; /* Previous free block */
WORD free_next; /* Next free block */
} LOCALARENA;
#define ARENA_HEADER_SIZE 4
#define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
/* Arena types (stored in 'prev' field of the arena) */
#define LOCAL_ARENA_FREE 0
#define LOCAL_ARENA_FIXED 1
/* LocalNotify() msgs */
#define LN_OUTOFMEM 0
#define LN_MOVE 1
#define LN_DISCARD 2
/* Layout of a handle entry table
*
* WORD count of entries
* LOCALHANDLEENTRY[count] entries
* WORD near ptr to next table
*/
typedef struct
{
WORD addr; /* Address of the MOVEABLE block */
BYTE flags; /* Flags for this block */
BYTE lock; /* Lock count */
} LOCALHANDLEENTRY;
/*
* We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
* in case something actually relies on this.
*
* An unused handle has lock = flags = 0xff. In windows addr is that of next
* free handle, at the moment in wine we set it to 0.
*
* A discarded block's handle has lock = addr = 0 and flags = 0x40
* (LMEM_DISCARDED >> 8)
*/
#define MOVEABLE_PREFIX sizeof(HLOCAL16)
#include "pshpack1.h"
typedef struct
{
WORD check; /* 00 Heap checking flag */
WORD freeze; /* 02 Heap frozen flag */
WORD items; /* 04 Count of items on the heap */
WORD first; /* 06 First item of the heap */
WORD pad1; /* 08 Always 0 */
WORD last; /* 0a Last item of the heap */
WORD pad2; /* 0c Always 0 */
BYTE ncompact; /* 0e Compactions counter */
BYTE dislevel; /* 0f Discard level */
DWORD distotal; /* 10 Total bytes discarded */
WORD htable; /* 14 Pointer to handle table */
WORD hfree; /* 16 Pointer to free handle table */
WORD hdelta; /* 18 Delta to expand the handle table */
WORD expand; /* 1a Pointer to expand function (unused) */
WORD pstat; /* 1c Pointer to status structure (unused) */
FARPROC16 notify; /* 1e Pointer to LocalNotify() function */
WORD lock; /* 22 Lock count for the heap */
WORD extra; /* 24 Extra bytes to allocate when expanding */
WORD minsize; /* 26 Minimum size of the heap */
WORD magic; /* 28 Magic number */
} LOCALHEAPINFO;
typedef struct
{
DWORD dwSize; /* 00 */
DWORD dwMemReserved; /* 04 */
DWORD dwMemCommitted; /* 08 */
DWORD dwTotalFree; /* 0C */
DWORD dwLargestFreeBlock; /* 10 */
DWORD dwcFreeHandles; /* 14 */
} LOCAL32INFO;
typedef struct
{
DWORD dwSize; /* 00 */
WORD hHandle; /* 04 */
DWORD dwAddress; /* 06 */
DWORD dwSizeBlock; /* 0A */
WORD wFlags; /* 0E */
WORD wType; /* 10 */
WORD hHeap; /* 12 */
WORD wHeapType; /* 14 */
DWORD dwNext; /* 16 */
DWORD dwNextAlt; /* 1A */
} LOCAL32ENTRY;
#include "poppack.h"
#define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
/* All local heap allocations are aligned on 4-byte boundaries */
#define LALIGN(word) (((word) + 3) & ~3)
#define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char *)(ptr)+(arena)))
#define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
#define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
#define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
/* determine whether the handle belongs to a fixed or a moveable block */
#define HANDLE_FIXED(handle) (((handle) & 3) == 0)
#define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
/* 32-bit heap definitions */
#define HTABLE_SIZE 0x10000
#define HTABLE_PAGESIZE 0x1000
#define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
#include "pshpack1.h"
typedef struct _LOCAL32HEADER
{
WORD freeListFirst[HTABLE_NPAGES];
WORD freeListSize[HTABLE_NPAGES];
WORD freeListLast[HTABLE_NPAGES];
DWORD selectorTableOffset;
WORD selectorTableSize;
WORD selectorDelta;
DWORD segment;
LPBYTE base;
DWORD limit;
DWORD flags;
DWORD magic;
HANDLE heap;
} LOCAL32HEADER;
#include "poppack.h"
#define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
static inline BOOL16 call_notify_func( FARPROC16 proc, WORD msg, HLOCAL16 handle, WORD arg )
{
DWORD ret;
WORD args[3];
if (!proc) return FALSE;
args[2] = msg;
args[1] = handle;
args[0] = arg;
WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
return LOWORD(ret);
}
/***********************************************************************
* LOCAL_GetHeap
*
* Return a pointer to the local heap, making sure it exists.
*/
static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
{
LOCALHEAPINFO *pInfo;
INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
if (!ptr || !ptr->heap) return NULL;
if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
{
WARN("Bad pointer\n");
return NULL;
}
pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
if (pInfo->magic != LOCAL_HEAP_MAGIC)
{
WARN("Bad magic\n");
return NULL;
}
return pInfo;
}
/***********************************************************************
* LOCAL_MakeBlockFree
*
* Make a block free, inserting it in the free-list.
* 'block' is the handle of the block arena; 'baseptr' points to
* the beginning of the data segment containing the heap.
*/
static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
{
LOCALARENA *pArena, *pNext;
WORD next;
/* Mark the block as free */
pArena = ARENA_PTR( baseptr, block );
pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
pArena->size = pArena->next - block;
/* Find the next free block (last block is always free) */
next = pArena->next;
for (;;)
{
pNext = ARENA_PTR( baseptr, next );
if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
next = pNext->next;
}
TRACE("%04x, next %04x\n", block, next );
/* Insert the free block in the free-list */
pArena->free_prev = pNext->free_prev;
pArena->free_next = next;
ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
pNext->free_prev = block;
}
/***********************************************************************
* LOCAL_RemoveFreeBlock
*
* Remove a block from the free-list.
* 'block' is the handle of the block arena; 'baseptr' points to
* the beginning of the data segment containing the heap.
*/
static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
{
/* Mark the block as fixed */
LOCALARENA *pArena = ARENA_PTR( baseptr, block );
pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
/* Remove it from the list */
ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
}
/***********************************************************************
* LOCAL_AddBlock
*
* Insert a new block in the heap.
* 'new' is the handle of the new block arena; 'baseptr' points to
* the beginning of the data segment containing the heap; 'prev' is
* the block before the new one.
*/
static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
{
LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
LOCALARENA *pNew = ARENA_PTR( baseptr, new );
pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
pNew->next = pPrev->next;
ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
ARENA_PTR(baseptr,pPrev->next)->prev |= new;
pPrev->next = new;
}
/***********************************************************************
* LOCAL_RemoveBlock
*
* Remove a block from the heap.
* 'block' is the handle of the block arena; 'baseptr' points to
* the beginning of the data segment containing the heap.
*/
static void LOCAL_RemoveBlock( char *baseptr, WORD block )
{
LOCALARENA *pArena, *pTmp;
/* Remove the block from the free-list */
TRACE("\n");
pArena = ARENA_PTR( baseptr, block );
if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
LOCAL_RemoveFreeBlock( baseptr, block );
/* If the previous block is free, expand its size */
pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
pTmp->size += pArena->next - block;
/* Remove the block from the linked list */
pTmp->next = pArena->next;
pTmp = ARENA_PTR( baseptr, pArena->next );
pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
}
/***********************************************************************
* LOCAL_PrintHeap
*/
static void LOCAL_PrintHeap( HANDLE16 ds )
{
char *ptr;
LOCALHEAPINFO *pInfo;
WORD arena;
/* FIXME - the test should be done when calling the function!
plus is not clear that we should print this info
only when TRACE_ON is on! */
if(!TRACE_ON(local)) return;
ptr = MapSL( MAKESEGPTR( ds, 0 ));
pInfo = LOCAL_GetHeap( ds );
if (!pInfo)
{
ERR( "Local Heap corrupted! ds=%04x\n", ds );
return;
}
TRACE( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
ds, pInfo->first, pInfo->last, pInfo->items );
arena = pInfo->first;
for (;;)
{
LOCALARENA *pArena = ARENA_PTR(ptr,arena);
TRACE( " %04x: prev=%04x next=%04x type=%d\n", arena,
pArena->prev & ~3, pArena->next, pArena->prev & 3 );
if (arena == pInfo->first)
{
TRACE( " size=%d free_prev=%04x free_next=%04x\n",
pArena->size, pArena->free_prev, pArena->free_next );
}
if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
{
TRACE( " size=%d free_prev=%04x free_next=%04x\n",
pArena->size, pArena->free_prev, pArena->free_next );
if (pArena->next == arena) break; /* last one */
if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
{
TRACE( "*** arena->free_next->free_prev != arena\n" );
break;
}
}
if (pArena->next == arena)
{
TRACE( "*** last block is not marked free\n" );
break;
}
if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
{
TRACE( "*** arena->next->prev != arena (%04x, %04x)\n",
pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
break;
}
arena = pArena->next;
}
}
#ifdef HEAP_VALIDATE
static BOOL LOCAL_Validate(WORD ds, HANDLE16 handle)
{
char *ptr;
LOCALHEAPINFO *pInfo;
ptr = MapSL(MAKESEGPTR(ds, 0));
if(!(pInfo = LOCAL_GetHeap(ds)))
return FALSE;
if (IsBadReadPtr16(MAKESEGPTR(ds, handle), 2))
goto fail;
if(HANDLE_MOVEABLE(handle))
{
LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
HANDLE16 nhandle = pEntry->addr - MOVEABLE_PREFIX;
if (ARENA_PTR(ptr, ARENA_HEADER(nhandle))->size != handle) goto fail;
handle = nhandle;
}
handle = ARENA_HEADER(handle);
if (ARENA_NEXT(ptr, ARENA_PREV(ptr, handle)) != handle) goto fail;
if (ARENA_PREV(ptr, ARENA_NEXT(ptr, handle)) != handle) goto fail;
return TRUE;
fail:
ERR("invalid handle %04x\n", handle);
return FALSE;
}
#endif
/***********************************************************************
* LocalInit (KERNEL.4)
*/
BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
{
char *ptr;
WORD heapInfoArena, freeArena, lastArena;
LOCALHEAPINFO *pHeapInfo;
LOCALARENA *pArena, *pFirstArena, *pLastArena;
BOOL16 ret = FALSE;
/* The initial layout of the heap is: */
/* - first arena (FIXED) */
/* - heap info structure (FIXED) */
/* - large free block (FREE) */
/* - last arena (FREE) */
TRACE("%04x %04x-%04x\n", selector, start, end);
if (!selector) selector = CURRENT_DS;
if (TRACE_ON(local))
{
/* If TRACE_ON(local) is set, the global heap blocks are */
/* cleared before use, so we can test for double initialization. */
if (LOCAL_GetHeap(selector))
{
ERR("Heap %04x initialized twice.\n", selector);
LOCAL_PrintHeap(selector);
}
}
if (start == 0)
{
/* start == 0 means: put the local heap at the end of the segment */
DWORD size = GlobalSize16( GlobalHandle16( selector ) );
start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
if ( end > 0xfffe ) end = 0xfffe;
start -= end;
end += start;
}
ptr = MapSL( MAKESEGPTR( selector, 0 ) );
start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
+ sizeof(LOCALHEAPINFO) );
lastArena = (end - sizeof(LOCALARENA)) & ~3;
/* Make sure there's enough space. */
if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
/* Initialise the first arena */
pFirstArena = ARENA_PTR( ptr, start );
pFirstArena->prev = start | LOCAL_ARENA_FIXED;
pFirstArena->next = heapInfoArena;
pFirstArena->size = LALIGN(sizeof(LOCALARENA));
pFirstArena->free_prev = start; /* this one */
pFirstArena->free_next = freeArena;
/* Initialise the arena of the heap info structure */
pArena = ARENA_PTR( ptr, heapInfoArena );
pArena->prev = start | LOCAL_ARENA_FIXED;
pArena->next = freeArena;
/* Initialise the heap info structure */
pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
pHeapInfo->items = 4;
pHeapInfo->first = start;
pHeapInfo->last = lastArena;
pHeapInfo->htable = 0;
pHeapInfo->hdelta = 0x20;
pHeapInfo->extra = 0x200;
pHeapInfo->minsize = lastArena - freeArena;
pHeapInfo->magic = LOCAL_HEAP_MAGIC;
/* Initialise the large free block */
pArena = ARENA_PTR( ptr, freeArena );
pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
pArena->next = lastArena;
pArena->size = lastArena - freeArena;
pArena->free_prev = start;
pArena->free_next = lastArena;
/* Initialise the last block */
pLastArena = ARENA_PTR( ptr, lastArena );
pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
pLastArena->next = lastArena; /* this one */
pLastArena->size = LALIGN(sizeof(LOCALARENA));
pLastArena->free_prev = freeArena;
pLastArena->free_next = lastArena; /* this one */
/* Store the local heap address in the instance data */
((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
LOCAL_PrintHeap( selector );
ret = TRUE;
done:
if (CURRENT_STACK16)
CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
return ret;
}
/***********************************************************************
* LOCAL_GrowHeap
*/
static BOOL16 LOCAL_GrowHeap( HANDLE16 ds, DWORD newsize )
{
HANDLE16 hseg;
LONG oldsize;
LONG end;
LOCALHEAPINFO *pHeapInfo;
WORD freeArena, lastArena;
LOCALARENA *pArena, *pLastArena;
char *ptr;
hseg = GlobalHandle16( ds );
/* maybe mem allocated by Virtual*() ? */
if (!hseg) return FALSE;
oldsize = GlobalSize16( hseg );
/* if nothing can be gained, return */
if (oldsize > 0xfff0) return FALSE;
hseg = GlobalReAlloc16( hseg, newsize, GMEM_FIXED );
if (!hseg)
return 0;
ptr = MapSL( MAKESEGPTR( ds, 0 ) );
pHeapInfo = LOCAL_GetHeap( ds );
if (pHeapInfo == NULL) {
ERR("Heap not found\n" );
return FALSE;
}
end = GlobalSize16( hseg );
lastArena = (end - sizeof(LOCALARENA)) & ~3;
/* Update the HeapInfo */
pHeapInfo->items++;
freeArena = pHeapInfo->last;
pHeapInfo->last = lastArena;
pHeapInfo->minsize += end - oldsize;
/* grow the old last block */
pArena = ARENA_PTR( ptr, freeArena );
pArena->size = lastArena - freeArena;
pArena->next = lastArena;
pArena->free_next = lastArena;
/* Initialise the new last block */
pLastArena = ARENA_PTR( ptr, lastArena );
pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
pLastArena->next = lastArena; /* this one */
pLastArena->size = LALIGN(sizeof(LOCALARENA));
pLastArena->free_prev = freeArena;
pLastArena->free_next = lastArena; /* this one */
/* If block before freeArena is also free then merge them */
if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
{
LOCAL_RemoveBlock(ptr, freeArena);
pHeapInfo->items--;
}
TRACE("Heap expanded\n" );
LOCAL_PrintHeap( ds );
return TRUE;
}
/***********************************************************************
* LOCAL_FreeArena
*/
static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALHEAPINFO *pInfo;
LOCALARENA *pArena, *pPrev;
TRACE("%04x ds=%04x\n", arena, ds );
if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
pArena = ARENA_PTR( ptr, arena );
if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
{
/* shouldn't happen */
ERR("Trying to free block %04x twice!\n",
arena );
LOCAL_PrintHeap( ds );
return arena;
}
/* Check if we can merge with the previous block */
pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
{
arena = pArena->prev & ~3;
pArena = pPrev;
LOCAL_RemoveBlock( ptr, pPrev->next );
pInfo->items--;
}
else /* Make a new free block */
{
LOCAL_MakeBlockFree( ptr, arena );
}
/* Check if we can merge with the next block */
if ((pArena->next == pArena->free_next) &&
(pArena->next != pInfo->last))
{
LOCAL_RemoveBlock( ptr, pArena->next );
pInfo->items--;
}
return 0;
}
/***********************************************************************
* LOCAL_ShrinkArena
*
* Shrink an arena by creating a free block at its end if possible.
* 'size' includes the arena header, and must be aligned.
*/
static WORD LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size, BOOL fromend )
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALARENA *pArena = ARENA_PTR( ptr, arena );
if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
{
LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
if (!pInfo) return;
pInfo->items++;
if (!fromend)
{
LOCAL_AddBlock( ptr, arena, arena + size );
LOCAL_FreeArena( ds, arena + size );
return arena;
}
else
{
LOCAL_AddBlock( ptr, arena, arena + pArena->size - size );
pArena->size -= size;
return arena + pArena->size;
}
}
return arena;
}
/***********************************************************************
* LOCAL_GrowArenaDownward
*
* Grow an arena downward by using the previous arena (must be free).
*/
static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALHEAPINFO *pInfo;
LOCALARENA *pArena = ARENA_PTR( ptr, arena );
WORD prevArena = pArena->prev & ~3;
LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
WORD offset, size;
char *p;
if (!(pInfo = LOCAL_GetHeap( ds ))) return;
offset = pPrevArena->size;
size = pArena->next - arena - ARENA_HEADER_SIZE;
LOCAL_RemoveFreeBlock( ptr, prevArena );
LOCAL_RemoveBlock( ptr, arena );
pInfo->items--;
p = (char *)pPrevArena + ARENA_HEADER_SIZE;
while (offset < size)
{
memcpy( p, p + offset, offset );
p += offset;
size -= offset;
}
if (size) memcpy( p, p + offset, size );
LOCAL_ShrinkArena( ds, prevArena, newsize, FALSE );
}
/***********************************************************************
* LOCAL_GrowArenaUpward
*
* Grow an arena upward by using the next arena (must be free and big
* enough). Newsize includes the arena header and must be aligned.
*/
static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALHEAPINFO *pInfo;
LOCALARENA *pArena = ARENA_PTR( ptr, arena );
WORD nextArena = pArena->next;
if (!(pInfo = LOCAL_GetHeap( ds ))) return;
LOCAL_RemoveBlock( ptr, nextArena );
pInfo->items--;
LOCAL_ShrinkArena( ds, arena, newsize, FALSE );
}
/***********************************************************************
* LOCAL_GetFreeSpace
*/
static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALHEAPINFO *pInfo;
LOCALARENA *pArena;
WORD arena;
WORD freespace = 0;
if (!(pInfo = LOCAL_GetHeap( ds )))
{
ERR("Local heap not found\n" );
LOCAL_PrintHeap(ds);
return 0;
}
arena = pInfo->first;
pArena = ARENA_PTR( ptr, arena );
while (arena != pArena->free_next)
{
arena = pArena->free_next;
pArena = ARENA_PTR( ptr, arena );
if (pArena->size >= freespace) freespace = pArena->size;
}
/* FIXME doesn't yet calculate space that would become free if everything
were discarded when countdiscard == 1 */
if (freespace < ARENA_HEADER_SIZE) freespace = 0;
else freespace -= ARENA_HEADER_SIZE;
return freespace;
}
/***********************************************************************
* LOCAL_Compact
*/
static UINT16 LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALHEAPINFO *pInfo;
LOCALARENA *pArena, *pMoveArena, *pFinalArena;
WORD arena, movearena, finalarena, table;
WORD count, movesize, size;
WORD freespace;
LOCALHANDLEENTRY *pEntry;
if (!(pInfo = LOCAL_GetHeap( ds )))
{
ERR("Local heap not found\n" );
LOCAL_PrintHeap(ds);
return 0;
}
TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
ds, minfree, flags);
freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
{
TRACE("Returning %04x.\n", freespace);
return freespace;
}
TRACE("Compacting heap %04x.\n", ds);
table = pInfo->htable;
while(table)
{
pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
{
if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
{
/* OK we can move this one if we want */
TRACE("handle %04x (block %04x) can be moved.\n",
(WORD)((char *)pEntry - ptr), pEntry->addr);
movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
pMoveArena = ARENA_PTR(ptr, movearena);
movesize = pMoveArena->next - movearena;
arena = pInfo->first;
pArena = ARENA_PTR(ptr, arena);
size = 0xffff;
finalarena = 0;
/* Try to find the smallest arena that will do, */
/* which is below us in memory */
for(;;)
{
arena = pArena->free_next;
pArena = ARENA_PTR(ptr, arena);
if(arena >= movearena)
break;
if(arena == pArena->free_next)
break;
if((pArena->size >= movesize) && (pArena->size < size))
{
size = pArena->size;
finalarena = arena;
}
}
if (finalarena) /* Actually got somewhere to move */
{
TRACE("Moving it to %04x.\n", finalarena);
pFinalArena = ARENA_PTR(ptr, finalarena);
size = pFinalArena->size;
LOCAL_RemoveFreeBlock(ptr, finalarena);
LOCAL_ShrinkArena( ds, finalarena, movesize, FALSE );
/* Copy the arena to its new location */
memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
(char *)pMoveArena + ARENA_HEADER_SIZE,
movesize - ARENA_HEADER_SIZE );
/* Free the old location */
LOCAL_FreeArena(ds, movearena);
call_notify_func(pInfo->notify, LN_MOVE,
(WORD)((char *)pEntry - ptr), pEntry->addr);
/* Update handle table entry */
pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
}
else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
== LOCAL_ARENA_FREE)
{
/* Previous arena is free (but < movesize) */
/* so we can 'slide' movearena down into it */
finalarena = pMoveArena->prev & ~3;
LOCAL_GrowArenaDownward( ds, movearena, movesize );
/* Update handle table entry */
pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
}
}
}
table = *(WORD *)pEntry;
}
freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
if(freespace >= minfree || (flags & LMEM_NODISCARD))
{
TRACE("Returning %04x.\n", freespace);
return freespace;
}
table = pInfo->htable;
while(table)
{
pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
{
if(pEntry->addr && pEntry->lock == 0 &&
(pEntry->flags & (LMEM_DISCARDABLE >> 8)))
{
TRACE("Discarding handle %04x (block %04x).\n",
(char *)pEntry - ptr, pEntry->addr);
LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
call_notify_func(pInfo->notify, LN_DISCARD, (char *)pEntry - ptr, pEntry->flags);
pEntry->addr = 0;
pEntry->flags = (LMEM_DISCARDED >> 8);
}
}
table = *(WORD *)pEntry;
}
return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
}
/***********************************************************************
* LOCAL_FindFreeBlock
*/
static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size, WORD flags )
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALHEAPINFO *pInfo;
LOCALARENA *pArena;
WORD arena, found = 0;
if (!(pInfo = LOCAL_GetHeap( ds )))
{
ERR("Local heap not found\n" );
LOCAL_PrintHeap(ds);
return 0;
}
arena = pInfo->first;
pArena = ARENA_PTR( ptr, arena );
for (;;) {
arena = pArena->free_next;
pArena = ARENA_PTR( ptr, arena );
if (arena == pArena->free_next) break;
if (pArena->size >= size) {
if (!(flags & LMEM_MOVEABLE)) return arena;
else found = arena;
}
}
if (found) return found;
TRACE("not enough space\n" );
LOCAL_PrintHeap(ds);
return 0;
}
/***********************************************************************
* get_heap_name
*/
static const char *get_heap_name( WORD ds )
{
HINSTANCE16 inst = LoadLibrary16( "GDI" );
if (ds == GlobalHandleToSel16( inst ))
{
FreeLibrary16( inst );
return "GDI";
}
FreeLibrary16( inst );
inst = LoadLibrary16( "USER" );
if (ds == GlobalHandleToSel16( inst ))
{
FreeLibrary16( inst );
return "USER";
}
FreeLibrary16( inst );
return "local";
}
SEGPTR WINAPI K32WOWGlobalLock16(HGLOBAL16 handle);
/***********************************************************************
* LOCAL_GetBlock
* The segment may get moved around in this function, so all callers
* should reset their pointer variables.
*/
static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, DWORD size, WORD flags )
{
char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
LOCALHEAPINFO *pInfo;
LOCALARENA *pArena;
WORD arena;
if (!(pInfo = LOCAL_GetHeap( ds )))
{
DWORD addr = K32WOWGlobalLock16(ds);
if (addr)
{
LocalInit16(ds, addr & 0xFFFF, (addr & 0xFFFF) + GlobalSize16(ds));
GlobalUnlock16(ds);
pInfo = LOCAL_GetHeap(ds);
}
else
{
ERR("Local heap not found\n");
LOCAL_PrintHeap(ds);
return 0;
}
}
size += ARENA_HEADER_SIZE;
size = LALIGN( max( size, sizeof(LOCALARENA) ) );
if (size >= 0x10000)
{
return 0;
}
#if 0