File: jmalloc.c

package info (click to toggle)
abuse 2.00-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 12,708 kB
  • ctags: 15,389
  • sloc: ansic: 115,852; cpp: 6,792; lisp: 2,066; sh: 1,734; makefile: 1,601; asm: 264
file content (923 lines) | stat: -rw-r--r-- 22,499 bytes parent folder | download | duplicates (3)
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//#include <new.h>

//#ifdef MEM_CHECK
//#define MEM_CLEAR
//#endif

#ifdef __WATCOMC__
#include "doscall.hpp"
#endif
#include "jmalloc.hpp"
#define uchar unsigned char
#define JM_SMALL_SIZE 128      // above 128 bytes is considered to be a big block and no hashing is done
int alloc_space=ALLOC_SPACE_STATIC;

extern void free_up_memory();

#ifdef MEM_CHECK
long break_mem_point=0;       // can be set in debugger, break mem fun will be called when this address is allocated
void break_mem_fun()
{
  printf("memory breakpoint\n");
}
#endif

struct memory_node
{
  long size;
#ifdef MEM_CHECK
  char *name;                     // name is allocated on regular heap
#endif                            // because it is used for debugging purposes
                                  // and will probably be run on my linux box with VMM
  memory_node *next;
};


struct small_block
{  
  unsigned long size;                     // size of blocks...
  unsigned long alloc_list;               // bit field saying weither each block is allocated or not.
  small_block *next;                      // next small block of same size
#ifdef MEM_CHECK
  char *name[32];
#endif 
} ;

enum { HI_BLOCK, LOW_BLOCK }; 

class block_manager
{
  public :

  long block_size;                             // size of this memory_block
  small_block *sblocks[JM_SMALL_SIZE];
  small_block *cblocks[JM_SMALL_SIZE];
  void *addr;

  memory_node *sfirst,*slast,
              *cfirst;
  unsigned char block_type;

  void init(void *block, long Block_size, uchar type);
  void *static_alloc(long size, char *name);
  void *cache_alloc(long size, char *name);
  void static_free(void *ptr);
  void cache_free(void *ptr);
  long available();
  long allocated();
  long pointer_size(void *ptr);
  void report(FILE *fp);
  void inspect();

  int valid_static_ptr(void *ptr);     // only called from within debugger
  int valid_cache_ptr(void *ptr);
} bmanage[5];

int bmanage_total=0;


void inspect_memory()
{
  for (int i=0;i<bmanage_total;i++)
    bmanage[i].inspect();
}


int block_manager::valid_static_ptr(void *ptr)
{
  void *next=(void *)(*(((long *)ptr)-1));
  if (next && ((small_block *)next)->size<JM_SMALL_SIZE)  // small allocation
  {
    small_block *s=(small_block *)next;
    if (s->size<=0) return 0;

    small_block *c=sblocks[s->size];
    while (c && c!=s) c=c->next;
    if (!c) return 0;
  }

  memory_node *o=(memory_node *)(((char *)ptr)-sizeof(memory_node));
  memory_node *f=sfirst;
  while (f && f!=o) f=f->next;
  if (f) return 1;
  else return 0;
}


int block_manager::valid_cache_ptr(void *ptr)
{
  void *next=(void *)(*(((long *)ptr)-1));
  if (next && ((small_block *)next)->size<JM_SMALL_SIZE)  // small allocation
  {
    small_block *s=(small_block *)next;
    if (s->size<=0) return 0;

    small_block *c=cblocks[s->size];
    while (c && c!=s) c=c->next;
    if (!c) return 0;
  }

  memory_node *o=(memory_node *)(((char *)ptr)-sizeof(memory_node));
  memory_node *f=cfirst;
  while (f && f!=o) f=f->next;
  if (f) return 1;
  else return 0;
}

void small_static_allocation_summary(int &total, int *&static_list, int *&cache_list)
{
  int size=1;
  total=JM_SMALL_SIZE/4;
  static_list=(int *)jmalloc(total*sizeof(int),"small static report");
  cache_list=(int *)jmalloc(total*sizeof(int),"small cache report");
  
  for (;size<total;size++)
  {
    static_list[size]=0;
    cache_list[size]=0;
    int i,x;
    for (i=0;i<bmanage_total;i++)
    {
      small_block *s=bmanage[i].sblocks[size];
      while (s) 
      { 
				for (x=0;x<32;x++)
				  if (s->alloc_list&(1<<x))
				    static_list[size]++; 
			
				s=s->next; 
      }

      s=bmanage[i].cblocks[size];
      while (s) 
      { 
				for (x=0;x<32;x++)
				  if (s->alloc_list&(1<<x))
				    cache_list[size]++; 
			
				s=s->next; 
      }
    }
  }
}

void block_manager::inspect()
{
  memory_node *f=sfirst;
  for (;f;f=f->next);               // scan through static big list

  int i,bit;
  for (i=0;i<JM_SMALL_SIZE;i++)
  {
    for (small_block *s=sblocks[i];s;s=s->next)
    {
      char *addr=((char *)(s+1));
      for (int j=0;j<32;j++)
      {
				if (s->alloc_list&bit)
				{
				  void *next=(void *)(*(((long *)addr)));
				  if ((long)next!=(long)s)
				  {
				    fprintf(stderr,"inspect : bad pointer\n");
				    return ;	    
				  }
				}
				bit=bit<<1;
				addr+=s->size+4;
      }	
    }
  }

}

void block_manager::report(FILE *fp)
{
  fprintf(fp,"************** Block size = %d ***************\n",block_size);
  fprintf(fp,"************** STATIC SPACE ******************\n");
  int i=0;
  memory_node *f=sfirst;
  for (;f;f=f->next,i++)
  {    
    fprintf(fp,"%4d   %p (%d) %4d      ",i,f,((char *)f-(char *)sfirst),f->size);
#ifdef MEM_CHECK
    if (f->size>0)
      fprintf(fp,"%s",f->name);
    else fprintf(fp,"FREE");
#endif
    fprintf(fp,"\n");
  }    
  for (i=0;i<JM_SMALL_SIZE;i++)
  {
    for (small_block *s=sblocks[i];s;s=s->next)
    {      
      fprintf(fp,"*** Small Block size = %d ***\n",i);      
      unsigned long x=0,bit=1;
      char *addr=((char *)(s+1));
      for (int j=0;j<32;j++)
      {
				fprintf(fp,"%p   ",addr);
				if (s->alloc_list&bit)
				{
#ifdef MEM_CHECK
				  fprintf(fp,"%s\n",s->name[j]);
#else
				  fprintf(fp,"allocated\n");
#endif	  
				} else fprintf(fp,"FREE\n");
				bit=bit<<1;
				addr+=s->size+4;
      }
    }
  }


  fprintf(fp,"************** CACHE SPACE ******************\n",block_size);
  i=0;
  for (f=cfirst;f;f=f->next,i++)
  {    
    fprintf(fp,"%4d   %p %4d      ",i,f,f->size);
#ifdef MEM_CHECK
    if (f->size>0)
      fprintf(fp,"%s",f->name);
    else fprintf(fp,"FREE");
#endif
    fprintf(fp,"\n");
  }    
  for (i=0;i<JM_SMALL_SIZE;i++)
  {
    for (small_block *s=cblocks[i];s;s=s->next)
    {      
      fprintf(fp,"*** Small Block size = %d ***\n",i);      
      unsigned long x=0,bit=1;
      char *addr=((char *)(s+1));
      for (int j=0;j<32;j++)
      {
	fprintf(fp,"%p   ",addr);
	if (s->alloc_list&bit)
	{
#ifdef MEM_CHECK
	  fprintf(fp,"%s\n",s->name[j]);
#else
	  fprintf(fp,"allocated\n");
#endif	  
	} else fprintf(fp,"FREE\n");
	bit=bit<<1;
	addr+=s->size+4;
      }
    }
  }
}

long block_manager::pointer_size(void *ptr)
{
  void *next=(void *)(*(((long *)ptr)-1));
  if (next>ptr)
    return ((memory_node *)(((char *)ptr)-sizeof(memory_node)))->size;
  else return ((small_block *)next)->size;
}

long block_manager::available()
{
  long size=0;
  memory_node *f;
  for (f=sfirst;f;f=f->next)
    if (f->size<0) size-=f->size;

  for (f=cfirst;f;f=f->next)
    if (f->size<0) size-=f->size;
  return size;
}

long block_manager::allocated()
{
  long size=0;
  memory_node *f;
  for (f=sfirst;f;f=f->next)
    if (f->size>0) size+=f->size;

  for (f=cfirst;f;f=f->next)
    if (f->size>0) size+=f->size;
  return size;
}

void block_manager::init(void *block, long Block_size, uchar type)
{
  block_size=Block_size;
  addr=block;
  /* 
     I'm padding each block, because I'm comparing pointers against size
     in jfree to determine weither a pointer is too a small object or a large alloc
     and it must always be true that the address of the pointer is > JM_SMALL_SIZE 
     All systems I know start pointer address pretty high, but this is a porting consern.     
  */
  
  slast=sfirst=(memory_node *)(((char *)block)+JM_SMALL_SIZE);   
  sfirst->size=-(block_size-sizeof(memory_node)-JM_SMALL_SIZE);
  sfirst->next=NULL;
  cfirst=NULL;
  memset(sblocks,0,sizeof(sblocks));
  memset(cblocks,0,sizeof(cblocks));
  block_type=type;
}

void *block_manager::static_alloc(long size, char *name)
{
  if (size<JM_SMALL_SIZE)
  {
    small_block *s=sblocks[size];
    for (;s && s->alloc_list==0xffffffff;s=s->next);
    if (!s)
    {
      s=(small_block *)static_alloc((size+4)*32+sizeof(small_block),"small_block");
      if (!s) return NULL;   // not enough room for another small block
      s->alloc_list=1;
      s->next=sblocks[size];
      sblocks[size]=s;
      s->size=size;
#ifdef MEM_CHECK
      s->name[0]=strcpy((char *)malloc(strlen(name)+1),name);
      if ((long)s==break_mem_point)
        break_mem_fun();
#endif      
      long *addr=(long *)(((char *)s)+sizeof(small_block));
      *addr=(long)s;
      return (void *)(addr+1);  // return first block
    } else
    {
      int bit=1,i=0,offset=0;
      char *addr=((char *)s)+sizeof(small_block);
      while (1)        // we already know there is a bit free
      {
	if ((s->alloc_list&bit)==0)
	{
	  s->alloc_list|=bit;
#ifdef MEM_CHECK
	  s->name[i]=strcpy((char *)malloc(strlen(name)+1),name);
#endif      	 
	  *((long *)addr)=(long)s;

#ifdef MEM_CHECK
	  if ((long)addr==break_mem_point)
            break_mem_fun();
#endif

	  return (void *)(addr+4);
	}
	i++;
	bit=bit<<1;
	addr+=size+4;
      }      
    }                
  }


  memory_node *s=sfirst;
  for (;s && -s->size<size;s=s->next);
  if (!s) return NULL;
  s->size=-s->size;

  if (s->size-size>sizeof(memory_node)+4)  // is there enough space to split the block?
  {    
    memory_node *p=(memory_node *)((char *)s+sizeof(memory_node)+size);
    if (s==slast)
      slast=p;
    p->size=-(s->size-size-sizeof(memory_node));
#ifdef MEM_CLEAR
//    memset( ((memory_node *)p)+1,0,-p->size);
#endif
    p->next=s->next;
    s->next=p;
    s->size=size;
  }
#ifdef MEM_CHECK
  s->name=strcpy((char *)malloc(strlen(name)+1),name);

  if ((long)s==break_mem_point)
    break_mem_fun();

#endif
  return (void *)(((char *)s)+sizeof(memory_node));
}


void *block_manager::cache_alloc(long size, char *name)
{
  if (size<JM_SMALL_SIZE)
  {
    small_block *s=cblocks[size];
    for (;s && s->alloc_list==0xffffffff;s=s->next);
    if (!s)
    {
      s=(small_block *)cache_alloc((size+4)*32+sizeof(small_block),"small_block");
      if (!s) return NULL;   // not enough room for another small block
      s->alloc_list=1;
      s->next=cblocks[size];
      cblocks[size]=s;
      s->size=size;
#ifdef MEM_CHECK
      s->name[0]=strcpy((char *)malloc(strlen(name)+1),name);

#endif      
      long *addr=(long *)(((char *)s)+sizeof(small_block));
      *addr=(long)s;
#ifdef MEM_CHECK
      if ((long)s==break_mem_point)
        break_mem_fun();
#endif
      return (void *)(addr+1);  // return first block
    } else
    {
      int bit=1,i=0,offset=0;
      char *addr=((char *)s)+sizeof(small_block);
      while (1)        // we already know there is a bit free
      {
	if ((s->alloc_list&bit)==0)
	{
	  s->alloc_list|=bit;
#ifdef MEM_CHECK
	  s->name[i]=strcpy((char *)malloc(strlen(name)+1),name);
	  if ((long)s==break_mem_point)
	    break_mem_fun();
#endif      	 
	  *((long *)addr)=(long)s;
	  return (void *)(addr+4);
	}
	i++;
	bit=bit<<1;
	addr+=size+4;
      }      
    }                
  }


  memory_node *clast=NULL;
  memory_node *s=cfirst;
  for (;s && -s->size<size;s=s->next) clast=s;
  if (!s) // no current cache space for object, see if we can enlarge the cache space
  {
    long size_avail=-slast->size;
    size_avail-=sizeof(memory_node);

    if (slast->size>0 || size_avail<size) // not enough space
      return NULL;
    else
    {
      slast->size+=size+sizeof(memory_node);
      memory_node *nc=(memory_node *)(((char *)(slast)) + (-slast->size+sizeof(memory_node)));
      
      nc->next=NULL;
      nc->size=size;
#ifdef MEM_CHECK
      nc->name=strcpy((char *)malloc(strlen(name)+1),name);      
      if ((long)nc==break_mem_point)
        break_mem_fun();
#endif      
      if (!clast)
        cfirst=nc;
      else clast->next=nc;
      return (void *)(((char *)nc)+sizeof(memory_node));
    }
  }


  s->size=-s->size;

  if (s->size-size>sizeof(memory_node)+4)  // is there enough space to split the block?
  {
    memory_node *p=s;    // store this position
    long psize=s->size-size-sizeof(memory_node);
    s=(memory_node *)(((char *)s)+psize+sizeof(memory_node));
    p->size=-psize;
    s->next=p;
    s->size=size;
    if (cfirst==p) cfirst=s;
    else clast->next=s;
  }
#ifdef MEM_CHECK
  s->name=strcpy((char *)malloc(strlen(name)+1),name);
  if ((long)s==break_mem_point)
    break_mem_fun();
#endif
  return (void *)(((char *)s)+sizeof(memory_node));
}


/************************** CACHE FREE ****************************/
/*    should be called to free a pointer in the cache heap        */
/*    i.e. end of the heap                                        */
/******************************************************************/
void block_manager::cache_free(void *ptr)
{
  // see if this was a small_block allocation
  void *next=(void *)(*(((long *)ptr)-1));
  if (next && ((small_block *)next)->size<JM_SMALL_SIZE)  // small allocation
  {
    small_block *s=(small_block *)next;
    if (s->size<=0)
    {
      fprintf(stderr,"jfree : bad pointer\n");
      return ;
    }

    int field=(((char *)ptr)-((char *)s)-sizeof(small_block))/(s->size+4);
#ifdef MEM_CHECK
    free(s->name[field]);
#endif
    s->alloc_list&=(0xffffffff-(1<<field));
    if (s->alloc_list==0)
    {
      small_block *l=NULL;
      small_block *n=cblocks[s->size];
      for (;n!=s;n=n->next) l=n;
#ifdef MEM_CHECK
      if (!n) 
      { printf("Free small block error\n"); }
#endif
      if (!l)
      cblocks[s->size]=s->next;
      else l->next=s->next;
      cache_free(s);
    }      
  } else
  {
    memory_node *o=(memory_node *)(((char *)ptr)-sizeof(memory_node)),*last=NULL;
    memory_node *n=cfirst;
    for (;n && n!=o;n=n->next) last=n;
#ifdef MEM_CHECK
    if (!n) 
    { printf("Free cached big block error\n"); }
    free(o->name);
#endif
    
    if (last && last->size<0)   // can we add into last block
    {
      memory_node *prev=NULL;
      for (memory_node *n=cfirst;n && n!=last;n=n->next) prev=n;   // find previous to last pointer
      if (prev)
        prev->next=o;
      else cfirst=o;

      o->size=last->size-o->size-sizeof(memory_node);
      last=prev;
    } else o->size=-o->size;
    
    if (!o->next)           // if no next block, then we should add back into static memory
    {
      if (last) last->next=NULL;  // unlink from cache chain
      else cfirst=NULL;

      if (slast->size>0)    // if last static is allocated then create a new free static at end of list
      {
	slast->next=o;
	slast=o;
      } else      
	slast->size+=o->size-sizeof(memory_node);  // else just increase the size of last block
    } else if (o->next->size<0)   // see if we can add into next block
    {
      o->next->size+=o->size-sizeof(memory_node);
      if (last)      
	last->next=o->next;
      else
        cfirst=o->next;
    }
  }  
}



/************************** STATIC FREE ***************************/
/*    should be called to free a pointer in the static heap       */
/*    i.e. begining of the heap                                   */
/******************************************************************/
void block_manager::static_free(void *ptr)
{
  // see if this was a small_block allocation
  void *next=(void *)(*(((long *)ptr)-1));
  if (next && next<ptr)  // small allocation
  {
    small_block *s=(small_block *)next;
    if (s->size<=0)
    {
      fprintf(stderr,"jfree : bad pointer\n");
      return ;
    }
#ifdef MEM_CLEAR
    memset(ptr,0,s->size);
#endif

    int field=(((char *)ptr)-((char *)s)-sizeof(small_block))/(s->size+4);
#ifdef MEM_CHECK
    free(s->name[field]);
#endif
    s->alloc_list&=(0xffffffff-(1<<field));
    if (s->alloc_list==0)
    {
      small_block *l=NULL;
      small_block *n=sblocks[s->size];
      for (;n!=s;n=n->next) l=n;
#ifdef MEM_CHECK
      if (!n) { printf("Free static small block error\n"); }
#endif
      if (!l)
      sblocks[s->size]=s->next;
      else l->next=s->next;
      static_free(s);
    }      
  } else
  {
    memory_node *o=(memory_node *)(((char *)ptr)-sizeof(memory_node)),*last=NULL;
#ifdef MEM_CHECK
    free(o->name);
#endif
#ifdef MEM_CLEAR
    memset(ptr,0,o->size);
#endif

    if (o->next && o->next->size<0)   // see if we can add into next block
    {
      if (o->next==slast)
        slast=o;
      o->size+=-o->next->size+sizeof(memory_node);
      o->next=o->next->next;
    }

    memory_node *n=sfirst;
    for (;n && n!=o;n=n->next) last=n;
#ifdef MEM_CHECK
    if (!n) { printf("Free static big block error\n"); }
#endif
    
    if (last && last->size<0)
    {
      if (o==slast) slast=last;
      last->next=o->next;
      last->size-=o->size+sizeof(memory_node);	
    } else o->size=-o->size;            
  }  
}


void jmalloc_uninit()
{
  for (int i=0;i<bmanage_total;i++)
  {
    switch (bmanage[i].block_type)
    {
      case HI_BLOCK : 
      { free(bmanage[i].addr); } break;
#ifdef __WATCOMC__      
      case LOW_BLOCK :
      { free_low_memory(bmanage[i].addr); } break;
#endif      
    }
  }
  bmanage_total=0;
}

void jmem_cleanup(int ret, void *arg)
{ jmalloc_uninit(); }

 
int jmalloc_max_size=3072000;
int jmalloc_min_low_size=0x1000;
char *not_enough_total_memory_message="Memory manager : Sorry you do not have enough memory available to\n"
                                       "                 run this program.\n"
				       "    DOS users  : Remove any TSR's and device drivers you can.\n"
				       "    UNIX users : Do you have a swapfile/partition setup?\n";
char *not_enough_low_memory_message="Memory Manager : Not enough low memory available (%d : need %d)\n"
                                   "  Suggestions...\n"
				   "    - make a boot disk\n"
				   "    - remove TSR's  & drivers not needed by ABUSE\n"
				   "    - add memory to your system\n";

void jmalloc_init(long min_size)
{
  if (bmanage_total)
    fprintf(stderr,"warning : jmalloc_init called twice\n");
  else
  {
//    exit_proc(jmem_cleanup,jmalloc_uninit);          // make sure memory gets freed up on exit
    void *mem;

#ifdef __POWERPC__
    long size=jmalloc_max_size-0x10000;
    for (mem=NULL;!mem && size>0x10000;)
    {
      mem=malloc(size+0x10000);
      if (!mem) size-=0x100;        
    }
    free(mem);
    mem = malloc(size);
#else
    long size=jmalloc_max_size;
    for (mem=NULL;!mem && size>0x4000;)
    {
      mem=malloc(size);
      if (!mem) size-=0x100;        
    }
#endif
    if (mem)
    {
			bmanage[bmanage_total].init(mem,size,HI_BLOCK);
			bmanage_total++;      
			fprintf(stderr,"Added himem block (%d bytes)\n",size);
    }

/*    bmanage[bmanage_total].init(malloc(2039552),2039552,HI_BLOCK);
    bmanage_total++;      
    bmanage[bmanage_total].init(malloc(150224),150224,HI_BLOCK);
    bmanage_total++;      */



#ifdef __WATCOMC__
    if (size!=jmalloc_max_size)
    {
      do
      {
				size=low_memory_available();
				if (size>jmalloc_min_low_size+0x1000)              // save 64K for misc low memory needs
				{
				  bmanage[bmanage_total].init(alloc_low_memory(size-jmalloc_min_low_size-0x1000),size-jmalloc_min_low_size-0x1000,LOW_BLOCK);
				  bmanage_total++; 
				  fprintf(stderr,"Added low memory block (%d bytes)\n",size);
				}
      } while (size>jmalloc_min_low_size+0x1000);
      if (size<jmalloc_min_low_size)
      {
				fprintf(stderr,not_enough_low_memory_message,size,jmalloc_min_low_size);
				exit(0);
      }
    }
#endif
 

    fprintf(stderr,"Memory available : %d\n",j_available());
    if (j_available()<min_size)
    {
      fprintf(stderr,not_enough_total_memory_message);
      exit(0);
    }

  }
}


long j_available()
{
  long size=0;
  for (int i=0;i<bmanage_total;i++) 
    size+=bmanage[i].available();
  return size;
}

long j_allocated()
{
  long size=0;
  for (int i=0;i<bmanage_total;i++) 
    size+=bmanage[i].allocated();
  return size;
}


void *jmalloc(long size, char *name)
{  
  if (!bmanage_total)
    return malloc(size);

  size=(size+3)&(0xffffffff-3);
  do
  {
    for (int i=0;i<bmanage_total;i++)
    {
      void *a;
      if (alloc_space==ALLOC_SPACE_STATIC)
        a=bmanage[i].static_alloc(size,name);
      else
        a=bmanage[i].cache_alloc(size,name);
      if (a) return a;
    }
    free_up_memory();
  } while (1);  
}

void jfree(void *ptr)
{
  if (ptr == NULL)
    return;
  if (!bmanage_total) 
  { 
    free(ptr); 
    return ; 
  }
  for (int i=0;i<bmanage_total;i++)
    if (ptr>=(void *)bmanage[i].sfirst)  // is the pointer in this block?
    {
      if (ptr<=(void *)bmanage[i].slast)  // is it in static space?
      {
	bmanage[i].static_free(ptr);
	return ;
      } else if (ptr<=(void *)(((char *)bmanage[i].sfirst)+bmanage[i].block_size))  // or cache space?
      {
	bmanage[i].cache_free(ptr);
	return ;
      }
    }

  free (ptr);  
//  fprintf(stderr,"jfree : bad pointer\n");
}


void *jrealloc(void *ptr, long size, char *name)
{  
  if (!ptr) return jmalloc(size,name);
  if (!bmanage_total) { return realloc(ptr,size); }

  if (size==0) { jfree(ptr); return NULL; }

  long old_size=0;
  for (int i=0;i<bmanage_total;i++)
    if (ptr>=(void *)bmanage[i].sfirst && 
	ptr<=(void *)(((char *)bmanage[i].sfirst)+bmanage[i].block_size))
    {
      old_size=bmanage[i].pointer_size(ptr);  
      if (ptr<=(void *)bmanage[i].slast)
      {
	int sp=alloc_space; sp=ALLOC_SPACE_STATIC;
	void *nptr=jmalloc(size,name);
	if (size>old_size)
	  memcpy(nptr,ptr,old_size);
 	else memcpy(nptr,ptr,size);
	bmanage[i].static_free(ptr);
	alloc_space=sp;
	return nptr;
      } else
      {
	int sp=alloc_space; sp=ALLOC_SPACE_CACHE;
	void *nptr=jmalloc(size,name);
	if (size>old_size)
	  memcpy(nptr,ptr,old_size);
	else memcpy(nptr,ptr,size);
	bmanage[i].cache_free(ptr);
	alloc_space=sp;
	return nptr;
      }
    }
  fprintf(stderr,"jrealloc : bad pointer\n");
  return NULL;
}

void dmem_report()
{
  mem_report("debug.mem");
}


void mem_report(char *filename)
{
  FILE *fp=fopen(filename,"wb");
  for (int i=0;i<bmanage_total;i++)
    bmanage[i].report(fp);
  fclose(fp);
}

void *operator new( size_t size)
{  
  return jmalloc(size,"::new object");
} 

void operator delete(void *ptr)
{
  jfree(ptr);
}


long small_ptr_size(void *ptr)
{
  return ((small_block *)(((long *)ptr)[-1]))->size;
}


int valid_ptr(void *ptr)
{
  if (!bmanage_total) { return 0; }
  for (int i=0;i<bmanage_total;i++)
    if (ptr>=(void *)bmanage[i].sfirst)  // is the pointer in this block?
    {
      if (ptr<=(void *)bmanage[i].slast)  // is it in static space?
      {
	return bmanage[i].valid_static_ptr(ptr);
      } else if (ptr<=(void *)(((char *)bmanage[i].sfirst)+bmanage[i].block_size))  // or cache space?
      {
	return bmanage[i].valid_cache_ptr(ptr);
      }
    }

  return 0;
}