-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
212 lines (182 loc) · 5.58 KB
/
misc.c
File metadata and controls
212 lines (182 loc) · 5.58 KB
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
/*
* =====================================================================================
*
* Filename: pg_misc.c
*
* Description:
*
* Version: 1.0
* Created: 01/06/2014 12:27:04 PM
* Revision: none
* Compiler: gcc
*
* Author: vyouzhi (),
* Organization:
*
* =====================================================================================
*/
#include "misc.h"
/*
* === FUNCTION ======================================================================
* Name: initdbp
* Description:
* =====================================================================================
*/
DBP *initdbp ( ){
DBP *_dbp;
_dbp = (DBP *)calloc(1, sizeof(DBP));
_dbp->inBuf = NULL;
_dbp->inBufSize = 0;
_dbp->inCursor = 0;
_dbp->inEnd = 0;
return _dbp;
} /* ----- end of function initdbp ----- */
/*
* === FUNCTION ======================================================================
* Name: freedbp
* Description:
* =====================================================================================
*/
void freedbp ( DBP *_dbp ){
if(!_dbp) return;
if(_dbp->inBuf) free(_dbp->inBuf);
_dbp->inBuf = NULL;
free(_dbp);
_dbp = NULL;
} /* ----- end of function freedbp ----- */
/*
* === FUNCTION ======================================================================
* Name: CheckBufSpace
* Description:
* =====================================================================================
*/
int CheckBufSpace ( ssize_t endPos, DBP *_dbp ){
char *newbuf;
if(!_dbp)return -1;
if(_dbp->inEnd != 0){
_dbp->inCursor = _dbp->inEnd;
}
_dbp->inEnd += endPos;
if(_dbp->inBufSize > _dbp->inEnd) return 0;
if(!_dbp->inBuf){
newbuf = (char *)calloc(_dbp->inEnd, sizeof(char));
}else{
newbuf = (char *)realloc(_dbp->inBuf, _dbp->inEnd);
}
_dbp->inBufSize = _dbp->inEnd;
if(newbuf){
_dbp->inBuf = newbuf;
} else{
freedbp(_dbp);
return -1;
}
return 0;
} /* ----- end of function CheckBufSpace ----- */
/*
* === FUNCTION ======================================================================
* Name: getInt
* Description:
* =====================================================================================
*/
int getInt ( int *result, size_t bytes, DBP *_dbp ){
uint32 t32;
uint16 t16;
switch ( bytes ) {
case 2:
memcpy(&t16, _dbp->inBuf + _dbp->inCursor, sizeof(uint16));
*result = (int)ntohs(t16);
break;
case 4:
memcpy(&t32, _dbp->inBuf + _dbp->inCursor, sizeof(uint32));
*result = (int)htonl(t32);
break;
default:
return -1;
} /* ----- end switch ----- */
return 0;
} /* ----- end of function getInt ----- */
/*
* === FUNCTION ======================================================================
* Name: getCont
* Description:
* =====================================================================================
*/
void getCont ( ){
return ;
} /* ----- end of function getCont ----- */
/*
* === FUNCTION ======================================================================
* Name: mcalloc
* Description:
* =====================================================================================
*/
void *mcalloc ( size_t nmemb, size_t size, const char *pathname, int flags ){
int fd;
void *start;
struct stat sb;
char name[1];
HFD *_hfd, *_hfd_next;
fd = open(pathname, flags);
fstat(fd, &sb);
if(sb.st_size==0){
DEBUG("init mmap file");
name[0]='\0';
write(fd, name, nmemb*size);
lseek(fd,0,SEEK_SET);
}
fstat(fd, &sb);
start = mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(start == MAP_FAILED){
DEBUG("init mmap error");
close(fd);
return NULL;
}
TAIL_HFD(_hfd_next);
_hfd = inithfd();
if(_hfd){
_hfd->fd = fd;
_hfd->fsize = sb.st_size;
_hfd_next->next = _hfd;
}
return start;
} /* ----- end of function mcalloc ----- */
/*
* === FUNCTION ======================================================================
* Name: inithfd
* Description:
* =====================================================================================
*/
HFD *inithfd ( ){
HFD *_hfd;
_hfd = (HFD *)calloc(1, sizeof(HFD));
if(!_hfd) return NULL;
_hfd->fd = 0;
_hfd->fsize = 0;
return _hfd;
} /* ----- end of function inithfd ----- */
/*
* === FUNCTION ======================================================================
* Name: freehfd
* Description:
* =====================================================================================
*/
void freehfd ( HFD *_hfd ){
if(_hfd->next){
freehfd(_hfd->next);
}
close(_hfd->fd);
free(_hfd);
} /* ----- end of function freehfd ----- */
/*
* === FUNCTION ======================================================================
* Name: buildCachePath
* Description:
* =====================================================================================
*/
char *buildCachePath (const char *name ){
char htab_path[FILE_PATH_LENGTH];
bzero(htab_path, FILE_PATH_LENGTH);
snprintf(htab_path, FILE_PATH_LENGTH-1, "%s/%s",conn_global->mmap_path, name);
return htab_path;
} /* ----- end of function buildCachePath ----- */
/* vim: set ts=4 sw=4: */