summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/third_party/nrappkit/src/util/libekr/r_memory.c
blob: 53846fc019895bfdee3858b8c8aac5e9ead38d7a (plain)
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
/**
   r_memory.c


   Copyright (C) 2004, Network Resonance, Inc.
   Copyright (C) 2006, Network Resonance, Inc.
   All Rights Reserved

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
   3. Neither the name of Network Resonance, Inc. nor the name of any
      contributors to this software may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.


   ekr@rtfm.com  Thu Apr 22 20:40:45 2004
 */

#include <string.h>
#include <stddef.h>
#include <assert.h>
#include "r_common.h"
#include "r_memory.h"

typedef struct r_malloc_chunk_ {
#ifdef SANITY_CHECKS
     UINT4 hdr;
#endif
     UCHAR type;
     UINT4 size;
     UCHAR memory[1];
} r_malloc_chunk;

#define CHUNK_MEMORY_OFFSET                    offsetof(struct r_malloc_chunk_, memory)
#define GET_CHUNK_ADDR_FROM_MEM_ADDR(memp) \
        ((struct r_malloc_chunk *)(((unsigned char*)(memp))-CHUNK_MEMORY_OFFSET))
#define CHUNK_SIZE(size) (size+sizeof(r_malloc_chunk))

#define HDR_FLAG 0x464c4147

static UINT4 mem_usage;      /* Includes our header */
static UINT4 mem_stats[256]; /* Does not include our header */

void *r_malloc(type,size)
  int type;
  size_t size;
  {
    size_t total;
    r_malloc_chunk *chunk;

    total=size+sizeof(r_malloc_chunk);

    if(!(chunk=malloc(total)))
      return(0);

#ifdef SANITY_CHECKS
    chunk->hdr=HDR_FLAG;
#endif
    chunk->type=type;
    chunk->size=size;

    mem_usage+=CHUNK_SIZE(size);
    mem_stats[type]+=size;

    return(chunk->memory);
  }

void *r_calloc(type,number,size)
  int type;
  size_t number;
  size_t size;
  {
    void *ret;
    size_t total;

    total=number*size;

    if(!(ret=r_malloc(type,total)))
      return(0);

    memset(ret,0,size);

    return(ret);
  }

void r_free(ptr)
  void *ptr;
  {
    r_malloc_chunk *chunk;

    if(!ptr) return;

    chunk=(r_malloc_chunk *)GET_CHUNK_ADDR_FROM_MEM_ADDR(ptr);
#ifdef SANITY_CHECKS
    assert(chunk->hdr==HDR_FLAG);
#endif

    mem_usage-=CHUNK_SIZE(chunk->size);
    mem_stats[chunk->type]-=chunk->size;

    free(chunk);
  }

void *r_realloc(ptr,size)
  void *ptr;
  size_t size;
  {
    r_malloc_chunk *chunk,*nchunk;
    size_t total;

    if(!ptr) return(r_malloc(255,size));

    chunk=(r_malloc_chunk *)GET_CHUNK_ADDR_FROM_MEM_ADDR(ptr);
#ifdef SANITY_CHECKS
    assert(chunk->hdr==HDR_FLAG);
#endif

    total=size + sizeof(r_malloc_chunk);

    if(!(nchunk=realloc(chunk,total)))
      return(0);

    mem_usage-=CHUNK_SIZE(nchunk->size);
    mem_stats[nchunk->type]-=nchunk->size;

    nchunk->size=size;
    mem_usage+=CHUNK_SIZE(nchunk->size);
    mem_stats[nchunk->type]+=nchunk->size;

    return(nchunk->memory);
  }

char *r_strdup(str)
  const char *str;
  {
    int len;
    char *nstr;

    if(!str)
      return(0);

    len=strlen(str)+1;

    if(!(nstr=r_malloc(0,len)))
      return(0);

    memcpy(nstr,str,len);

    return(nstr);
  }

int r_mem_get_usage(usagep)
  UINT4 *usagep;
  {
    *usagep=mem_usage;

    return(0);
  }

int r_memory_dump_stats()
  {
    int i;

    printf("Total memory usage: %d\n",mem_usage);
    printf("Memory usage by bucket\n");
    for(i=0;i<256;i++){
      if(mem_stats[i]){
        printf("%d\t%d\n",i,mem_stats[i]);
      }
    }
    return(0);
  }

void *r_malloc_compat(size)
  size_t size;
  {
    return(r_malloc(255,size));
  }