Subversion Repositories Kolibri OS

Rev

Rev 300 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 300 Rev 324
Line 1... Line 1...
1
#include "mf.h"
1
#include "mf.h"
-
 
2
#include "kolibri.h"
-
 
3
 
2
void * memcpy(void * dst, void * src, size_t count);
4
void * kmemcpy(void * dst, const void * src, size_t count);
Line 3... Line 5...
3
 
5
 
Line 4... Line 6...
4
static struct m_state ms;
6
static struct m_state ms;
5
 
7
 
6
void init_malloc(void* p)
8
void mf_init()
7
{ mchunkptr chp;
9
{ mchunkptr chp;
Line 8... Line 10...
8
  int i;
10
  int i;
-
 
11
  char  *p;
9
  dword psize= 0x10000;
12
  
10
  
13
  dword psize=  0x40000;
-
 
14
  p = 0x2600000;      //(char*)UserAlloc(psize);
-
 
15
  ms.top = (mchunkptr)p;
Line 11... Line 16...
11
  psize-= 40;
16
  ms.topsize = psize;
12
  ms.top = (mchunkptr)p;
17
  ms.smallmap=0;
Line 13... Line 18...
13
  ms.topsize = psize;
18
  ms.treemap=0;
Line 23... Line 28...
23
   
28
   
Line 24... Line 29...
24
  };   
29
  };   
25
}
30
}
Line 26... Line 31...
26
 
31
 
27
void *malloc(size_t size)
32
void *dlmalloc(size_t size)
28
{ size_t nb, psize,rsize;
33
{ size_t nb, psize,rsize;
29
  dword idx;
34
  dword idx;
30
  dword smallbits;
35
  dword smallbits;
31
  mchunkptr B,F,p,r;
36
  mchunkptr B,F,p,r;
Line 32... Line 37...
32
  void *mem;
37
  void *mem;
Line 33... Line 38...
33
 
38
 
34
  nb = ((size+7)&~7)+8;   
39
  nb = ((size+7)&~7)+8;   
35
  
40
  
36
  if (nb < 256)
41
  if (nb < 256)
37
  {
42
  {
38
//    idx = nb  >> 3;
43
    idx = nb  >> 3;
39
//    smallbits= (-1<
44
//    smallbits= (-1<
40
    _asm
45
    _asm
41
    {
46
    {
42
      mov ecx, [nb]
47
      mov ecx, [idx]
43
      or eax, -1
48
      or eax, -1
44
      shl eax, cl
49
      shl eax, cl
45
      and eax, [ms.smallmap] 
50
      and eax, dword ptr [ms] 
46
      mov [smallbits], eax    
51
      mov dword ptr [smallbits], eax    
47
    }
52
    }
48
    if (smallbits)
53
    if (smallbits)
49
    {
54
    {
50
      _asm
55
      _asm
Line 51... Line 56...
51
      { bsf eax, [smallbits]
56
      { bsf eax, dword ptr [smallbits]
52
        mov [idx], eax   
57
        mov [idx], eax   
Line 63... Line 68...
63
//        ms.smallmap &=  ~(1<< idx);
68
//        ms.smallmap &=  ~(1<< idx);
64
        _asm
69
        _asm
65
        {
70
        {
66
          mov eax, [idx]
71
          mov eax, [idx]
67
          btr [ms.smallmap], eax
72
          btr dword ptr [ms], eax
68
        }
73
        }
69
      }  
74
      }  
70
      B->fd = F;
75
      B->fd = F;
71
      F->bk = B;
76
      F->bk = B;
72
 
77
 
Line 117... Line 122...
117
 
122
 
Line 118... Line 123...
118
  return 0; 
123
  return 0; 
119
};
124
};
Line 120... Line 125...
120
 
125
 
121
void free(void *mem)
126
void dlfree(void *mem)
122
{ size_t psize;
127
{ size_t psize;
123
  size_t prevsize;
128
  size_t prevsize;
124
  size_t nsize;
129
  size_t nsize;
125
  mchunkptr next;
130
  mchunkptr next;
Line 307... Line 312...
307
  size_t rsize;
312
  size_t rsize;
308
  dword i;
313
  dword i;
309
  
314
  
Line 310... Line 315...
310
  _asm
315
  _asm
311
  { bsf ecx,[ms.treemap]
316
  { bsf ecx,dword ptr [ms+4]
312
    mov [i], ecx
317
    mov [i], ecx
313
  }  
318
  }  
314
  v = t =  ms.treebins[i];
319
  v = t =  ms.treebins[i];
315
  rsize = (t->head & ~INUSE_BITS) - nb;
320
  rsize = (t->head & ~INUSE_BITS) - nb;
Line 479... Line 484...
479
  if (v != 0)
484
  if (v != 0)
480
  {
485
  {
481
    mchunkptr r = chunk_plus_offset((mchunkptr)v, nb);
486
    mchunkptr r = chunk_plus_offset((mchunkptr)v, nb);
482
    unlink_large_chunk(v);
487
    unlink_large_chunk(v);
483
    if (rsize < 256)
488
    if (rsize < 16)
484
    {      
489
    {      
485
      v->head = (rsize + nb)|PINUSE_BIT|CINUSE_BIT;
490
      v->head = (rsize + nb)|PINUSE_BIT|CINUSE_BIT;
486
      ((mchunkptr)((char*)v+rsize + nb))->head |= PINUSE_BIT;
491
      ((mchunkptr)((char*)v+rsize + nb))->head |= PINUSE_BIT;
487
    }  
492
    }  
488
    else
493
    else
489
    {
494
    {
490
      v->head = nb|PINUSE_BIT|CINUSE_BIT;
495
      v->head = nb|PINUSE_BIT|CINUSE_BIT;
491
      r->head = rsize|PINUSE_BIT;
496
      r->head = rsize|PINUSE_BIT;
492
      ((mchunkptr)((char*)r+rsize))->prev_foot = rsize;
497
      ((mchunkptr)((char*)r+rsize))->prev_foot = rsize;
493
      insert_large_chunk((tchunkptr)r, rsize);
498
      insert_chunk((mchunkptr)r, rsize);
494
    }
499
    }
495
    return chunk2mem(v);
500
    return chunk2mem(v);
496
  }
501
  }
497
  return 0;
502
  return 0;
498
}
503
}
499
 
504
 
Line 500... Line 505...
500
static void* internal_realloc(struct m_state *m, void* oldmem,
505
static void* internal_realloc(struct m_state *m, void* oldmem,
501
                               size_t bytes);
506
                               size_t bytes);
Line 502... Line 507...
502
 
507
 
503
void* realloc(void* oldmem, size_t bytes)
508
void* dlrealloc(void* oldmem, size_t bytes)
504
{
509
{
505
  if (oldmem == 0)
510
  if (oldmem == 0)
506
    return malloc(bytes);
511
    return dlmalloc(bytes);
507
  else
512
  else
508
  {
513
  {
509
    struct m_state *m = &ms;
514
    struct m_state *m = &ms;
510
    return internal_realloc(m, oldmem, bytes);
515
    return internal_realloc(m, oldmem, bytes);
Line 575... Line 580...
575
 
580
 
576
  if (newp != 0)
581
  if (newp != 0)
577
  {
582
  {
578
    if (extra != 0) 
583
    if (extra != 0) 
Line 579... Line 584...
579
       free(extra);
584
       dlfree(extra);
580
      
585
      
581
    check_inuse_chunk(m, newp);
586
    check_inuse_chunk(m, newp);
582
    return chunk2mem(newp);
587
    return chunk2mem(newp);
583
  }
588
  }
584
  else
589
  else
585
  {
590
  {
586
    void* newmem = malloc(bytes);
591
    void* newmem = dlmalloc(bytes);
587
    if (newmem != 0)
592
    if (newmem != 0)
588
    {
593
    {
589
       size_t oc = oldsize - 4;
594
       size_t oc = oldsize - 4;
590
       memcpy(newmem, oldmem, (oc < bytes)? oc : bytes);
595
       memcpy(newmem, oldmem, (oc < bytes)? oc : bytes);
591
       free(oldmem);
596
       dlfree(oldmem);
592
    }
597
    }
593
    return newmem;
598
    return newmem;
594
  }
599
  }