File: vla.c

package info (click to toggle)
pymol 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,288 kB
  • sloc: cpp: 476,472; python: 76,538; ansic: 29,510; javascript: 6,792; sh: 47; makefile: 24
file content (157 lines) | stat: -rw-r--r-- 3,885 bytes parent folder | download | duplicates (4)
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
/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
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* -------------------------------------------------------------------
*/
/* This file can be compiled under C as a .c file, or under C++ as a .cc file*/

#include<stdio.h>

#include"vla.h"


#ifndef _os_memory_debug_on
void *VLAExpand(void *ptr,unsigned int rec)
#else
void *_champVLAExpand(const char *file,int line,
                void *ptr,unsigned int rec)
#endif
{
  VLARec *vla;
  char *start,*stop;
  unsigned int soffset=0;
  vla = &(((VLARec*)ptr)[-1]);
  if(rec>=vla->nAlloc)
	 {
		if(vla->autoZero)
		  soffset = sizeof(VLARec)+(vla->recSize*vla->nAlloc);
		vla->nAlloc = (rec*(vla->growFactor+10)/10)+1;
#ifndef _os_memory_debug_on
		vla=(void*)os_realloc(vla,(vla->recSize*vla->nAlloc)+sizeof(VLARec));
#else
		vla=(void*)OSMemoryRealloc(vla,(vla->recSize*vla->nAlloc)+sizeof(VLARec),file,line,_OSMemoryVLA);
#endif

		if(!vla)
		  {
			 printf("VLAExpand-ERR: realloc failed\n");
			 exit(EXIT_FAILURE);
		  }
		if(vla->autoZero)
		  {
			 start = ((char*)vla) + soffset;
			 stop = ((char*)vla)+sizeof(VLARec)+(vla->recSize*vla->nAlloc);
			 os_zero(start,stop);
		  }
	 }
  return((void*)&(vla[1]));
}

#ifndef _os_memory_debug_on
void *VLAMalloc(unsigned int initSize,unsigned int recSize,unsigned int growFactor,int autoZero)
#else
void *_champVLAMalloc(const char *file,int line,unsigned int initSize,unsigned int recSize,unsigned int growFactor,int autoZero)
#endif
{
  VLARec *vla;
#ifndef _os_memory_debug_on
  if(autoZero)
    vla=(void*)os_calloc(1,(initSize*recSize)+sizeof(VLARec));
  else
    vla=(void*)os_malloc((initSize*recSize)+sizeof(VLARec));
#else
  if(autoZero)
    vla=OSMemoryCalloc(1,(initSize*recSize)+sizeof(VLARec),file,line,_OSMemoryVLA);
  else
    vla=OSMemoryMalloc((initSize*recSize)+sizeof(VLARec),file,line,_OSMemoryVLA);
#endif

  if(!vla)
	 {
		printf("VLAMalloc-ERR: realloc failed\n");
		exit(EXIT_FAILURE);
	 }
  vla->nAlloc=initSize;
  vla->recSize=recSize;
  vla->growFactor=growFactor;
  vla->autoZero=autoZero;
  return((void*)&(vla[1]));
}

#ifndef _os_memory_debug_on
void VLAFree(void *ptr)
#else
void _champVLAFree(const char *file,int line,void *ptr)
#endif
{
  VLARec *vla;
  vla = &(((VLARec*)ptr)[-1]);
#ifndef _os_memory_debug_on
  os_free(vla);
#else
  OSMemoryFree(vla,file,line,_OSMemoryVLA);
#endif
}

unsigned int VLAGetSize2(void *ptr)
{
  VLARec *vla;
  vla = &((VLARec*)ptr)[-1];
  return(vla->nAlloc);
}

#ifndef _os_memory_debug_on
void *VLASetSize(void *ptr,unsigned int newSize)
#else
void *_champVLASetSize(const char *file,int line,void *ptr,unsigned int newSize)
#endif
{
  VLARec *vla;
  unsigned int orig_size = 0;
  char *start;
  char *stop;
  vla = &((VLARec*)ptr)[-1];
  if(vla->autoZero)
	 orig_size = sizeof(VLARec)+(vla->recSize*vla->nAlloc);
  vla->nAlloc = newSize;
#ifndef _os_memory_debug_on
  vla=(void*)os_realloc(vla,(vla->recSize*vla->nAlloc)+sizeof(VLARec));
#else
  vla=OSMemoryRealloc(vla,(vla->recSize*vla->nAlloc)+sizeof(VLARec),file,line,_OSMemoryVLA);
#endif

  if(!vla)
	 {
		printf("VLASetSize-ERR: realloc failed\n");
		exit(EXIT_FAILURE);
	 }
  if(vla->autoZero)
	 {
      start = ((char*)vla)+orig_size;
		stop = ((char*)vla)+sizeof(VLARec)+(vla->recSize*vla->nAlloc);
		if(start<stop)
		  os_zero(start,stop);
	 }
  return((void*)&(vla[1]));
}