ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Urlader/urlib.c
(Generate patch)

Comparing Urlader/urlib.c (file contents):
Revision 1.2 by root, Mon Jan 2 09:23:04 2012 UTC vs.
Revision 1.3 by root, Fri Jan 6 07:56:36 2012 UTC

110static void * 110static void *
111u_malloc (unsigned int size) 111u_malloc (unsigned int size)
112{ 112{
113 void *addr; 113 void *addr;
114 114
115 if (!size)
116 return 0;
117
115#ifdef _WIN32 118#ifdef _WIN32
116 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL); 119 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
117 120
121 addr = 0;
118 if (!handle) 122 if (handle)
119 return 0; 123 {
120
121 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size); 124 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size);
122
123 CloseHandle (handle); 125 CloseHandle (handle);
126 }
124#elif defined (MAP_ANONYMOUS) 127#elif defined (MAP_ANONYMOUS)
125 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); 128 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
126 129
127 if (addr == (void *)-1) 130 if (addr == (void *)-1)
128 addr = 0; 131 addr = 0;
129#else 132#else
130 addr = malloc (size); 133 addr = malloc (size);
131#endif 134#endif
132 135
136 if (!addr)
137 u_fatal ("memory allocation failure, aborting.");
138
133 return addr; 139 return addr;
134} 140}
135 141
136static void 142static void
137u_free (void *addr, unsigned int size) 143u_free (void *addr, unsigned int size)
138{ 144{
145 if (!addr)
146 return;
147
139#ifdef _WIN32 148#ifdef _WIN32
140 UnmapViewOfFile (addr); 149 UnmapViewOfFile (addr);
141#elif defined (MAP_ANONYMOUS) 150#elif defined (MAP_ANONYMOUS)
142 munmap (addr, size); 151 munmap (addr, size);
143#else 152#else
144 free (addr); 153 free (addr);
145#endif 154#endif
146} 155}
147 156
148static void * 157static void *
158u_realloc (void *addr, unsigned int old_size, unsigned int new_size)
159{
160 void *addr2 = u_malloc (new_size);
161 memcpy (addr2, addr, (new_size < old_size ? new_size : old_size));
162 u_free (addr, old_size);
163
164 return addr2;
165}
166
167static void *
149u_mmap (u_handle h, unsigned int size) 168u_mmap (u_handle h, unsigned int size)
150{ 169{
151 void *addr; 170 void *addr;
152 171
153#ifdef _WIN32 172#ifdef _WIN32
175#ifdef _WIN32 194#ifdef _WIN32
176 UnmapViewOfFile (addr); 195 UnmapViewOfFile (addr);
177#else 196#else
178 munmap (addr, len); 197 munmap (addr, len);
179#endif 198#endif
199}
200
201/////////////////////////////////////////////////////////////////////////////
202
203typedef struct
204{
205 char *addr;
206 unsigned int used;
207 unsigned int size;
208} u_dynbuf;
209
210static void *
211u_dynbuf_append (u_dynbuf *dynbuf, void *data, unsigned int len)
212{
213 char *dest;
214
215 if ((dynbuf->used += len) > dynbuf->size)
216 {
217 unsigned int new_size = dynbuf->size ? dynbuf->size * 2 : 4096;
218 dynbuf->addr = u_realloc (dynbuf->addr, dynbuf->size, new_size);
219 dynbuf->size = new_size;
220 }
221
222 dest = dynbuf->addr + dynbuf->used - len;
223
224 if (data)
225 memcpy (dest, data, len);
226
227 return dest;
180} 228}
181 229
182///////////////////////////////////////////////////////////////////////////// 230/////////////////////////////////////////////////////////////////////////////
183 231
184static void 232static void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines