File: MemoryCache.cpp

package info (click to toggle)
pymol 1.8.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,248 kB
  • ctags: 24,095
  • sloc: cpp: 474,635; python: 75,034; ansic: 22,888; sh: 236; makefile: 78; csh: 21
file content (196 lines) | stat: -rw-r--r-- 5,582 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


/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* Copyright (c) Schrodinger, LLC. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/

#include"MemoryCache.h"

#ifdef _MemoryCache_ON

#include"MemoryDebug.h"
#include"Setting.h"

typedef struct {
  int *ptr;
  unsigned int size;
} MemoryCacheRec;

typedef MemoryCacheRec MemoryCacheThread[cMemoryCache_max_block];

struct _CMemoryCache {
  MemoryCacheThread Cache[cMemoryCache_max_group];
};

void MemoryCacheInit(PyMOLGlobals * G)
{
  G->MemoryCache = Calloc(CMemoryCache, 1);
}

void *_MemoryCacheMalloc(PyMOLGlobals * G, unsigned int size, int group_id,
                         int block_id MD_FILE_LINE_Decl)
{
  if((group_id < 0) || (!SettingGetGlobal_b(G, cSetting_cache_memory)))
    return (mmalloc(size));

  {
    CMemoryCache *I = G->MemoryCache;
    MemoryCacheRec *rec = &I->Cache[group_id][block_id];

    if(!rec->ptr) {
      rec->size = size;
      rec->ptr = mmalloc(size);
    } else if(rec->size < size) {
      rec->size = size;
      mfree(rec->ptr);
      rec->ptr = mmalloc(size);
    }
    return (rec->ptr);
  }
}

void *_MemoryCacheCalloc(PyMOLGlobals * G, unsigned int number, unsigned int size,
                         int group_id, int block_id MD_FILE_LINE_Decl)
{
  if((group_id < 0) || (!SettingGetGlobal_b(G, cSetting_cache_memory)))
    return (mcalloc(number, size));

  {
    CMemoryCache *I = G->MemoryCache;
    MemoryCacheRec *rec = &I->Cache[group_id][block_id];
    unsigned int true_size = number * size;

    /* interesting result: calloc is faster than cacheing */

    if(!rec->ptr) {
      rec->size = true_size;
      rec->ptr = mcalloc(number, size);
    } else if(rec->size < true_size) {
      mfree(rec->ptr);
      rec->size = true_size;
      rec->ptr = mcalloc(number, size);
    } else {
      mfree(rec->ptr);
      rec->size = true_size;
      rec->ptr = mcalloc(number, size);
    }
    return (rec->ptr);
  }
}

void MemoryCacheReplaceBlock(PyMOLGlobals * G, void *ptr, int group_id, int old_block_id,
                             int new_block_id)
{
  if((group_id < 0) || (!SettingGetGlobal_b(G, cSetting_cache_memory)))
    return;
  {
    CMemoryCache *I = G->MemoryCache;
    MemoryCacheRec *old_rec = &I->Cache[group_id][old_block_id];
    MemoryCacheRec *new_rec = &I->Cache[group_id][new_block_id];
    if(new_rec->ptr)
      mfree(new_new->ptr);
    *(new_rec) = *(old_rec);
    old_rec->ptr = NULL;
  }
}

void *_MemoryCacheRealloc(PyMOLGlobals * G, void *ptr, unsigned int size, int group_id,
                          int block_id MD_FILE_LINE_Decl)
{
  /* no checking done */

  if((group_id < 0) || (!SettingGetGlobal_b(G, cSetting_cache_memory)))
    return (mrealloc(ptr, size));
  {
    CMemoryCache *I = G->MemoryCache;
    MemoryCacheRec *rec = &I->Cache[group_id][block_id];

    if(ptr != rec->ptr)
      printf("Error: Memory Cache Mismatch 2 %d %d\n", group_id, block_id);
    if(!rec->ptr) {
      rec->size = size;
      rec->ptr = mrealloc(ptr, size);
    } else if(rec->size < size) {
      rec->size = size;
      rec->ptr = mrealloc(ptr, size);
    }
    return (rec->ptr);
  }
}

void *_MemoryShrinkForSure(PyMOLGlobals * G, void *ptr, unsigned int size, int group_id,
                           int block_id MD_FILE_LINE_Decl)
{
  /* no checking done */

  if((group_id < 0) || (!SettingGetGlobal_b(G, cSetting_cache_memory)))
    return (ReallocForSure(ptr, size)); /* NOTE: fatal if new ptr is larger than old... */
  {
    CMemoryCache *I = G->MemoryCache;
    MemoryCacheRec *rec = &I->Cache[group_id][block_id];

    if(ptr != rec->ptr)
      printf("Error: Memory Cache Mismatch 2 %d %d\n", group_id, block_id);
    if(!rec->ptr) {             /* not currently cache-allocated... this should never happen */
      rec->size = size;
      rec->ptr = mrealloc(ptr, size);
    } else if(rec->size < size) {
      rec->ptr = MemoryReallocForSureSafe(ptr, size, rec->size);
      rec->size = size;
    } else {                    /* expanding size...should never happen... this should never happen */
      rec->size = size;
      rec->ptr = mrealloc(ptr, size);
    }
    return (rec->ptr);
  }
}

void _MemoryCacheFree(PyMOLGlobals * G, void *ptr, int group_id, int block_id,
                      int force MD_FILE_LINE_Decl)
{
  if((group_id < 0) || (!SettingGetGlobal_b(G, cSetting_cache_memory))) {
    mfree(ptr);
    return;
  }
  {
    CMemoryCache *I = G->MemoryCache;
    MemoryCacheRec *rec = &I->Cache[group_id][block_id];
    if(rec->ptr && (ptr != rec->ptr))
      printf("Error: Memory Cache Mismatch 2 %d %d\n", group_id, block_id);
    if(force) {
      if(rec->ptr)
        mfree(rec->ptr);
      rec->ptr = NULL;
    }
  }
}

void MemoryCacheDone(PyMOLGlobals * G)
{
  int a, b;
  CMemoryCache *I = G->MemoryCache;
  for(a = 0; a < cMemoryCache_max_group; a++) {
    for(b = 0; b < cMemoryCache_max_block; b++) {
      MemoryCacheRec *rec = &I->Cache[a][b];
      if(rec->ptr)
        mfree(rec->ptr);
    }
  }
  FreeP(G->MemoryCache);
}
#else
typedef int file_not_empty_as_per_iso_c;

#endif