File: memory.c

package info (click to toggle)
gif2png 0.6-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 152 kB
  • ctags: 137
  • sloc: ansic: 1,319; makefile: 62; sh: 17
file content (227 lines) | stat: -rw-r--r-- 4,511 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * memory.c: memory storage for gif conversion
 * Copyright (C) 1995 Alexander Lehmann
 * For conditions of distribution and use, see copyright notice in gif2png.h
 */

/*
 * currently two modes are supported:
 * - (default) store complete images in continuos blocks in memory, this works
 *   with systems that support virtual memory, e.g. unix, djgpp, etc.
 * - (-DTMPFILE) store the image data in a temporary file, this should work on
 *  any system regardless of memory restrictions, but may be significantly
 *  slower than the full memory version.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "gif2png.h"

#ifdef TMPFILE
FILE *tempfile;
#endif

void *xalloc(unsigned long s)
{
  void *p=malloc((size_t)s);

  if(p==NULL) {
    fprintf(stderr, "fatal error, out of memory\n");
    fprintf(stderr, "exiting ungracefully\n");
    exit(1);
  }

  return p;
}

void *xrealloc(void *p, unsigned long s)
{
  p=realloc(p,(size_t)s);

  if(p==NULL) {
    fprintf(stderr, "fatal error, out of memory\n");
    fprintf(stderr, "exiting ungracefully\n");
    exit(1);
  }

  return p;
}

#ifndef TMPFILE

/* assume unlimited memory */

/* allocate a new GIFelement, advance current pointer and initialize
   size to 0 */

#define ALLOCSIZE 8192

void allocate_element(void)
{
  struct GIFelement *new=xalloc(sizeof(*new));

  memset(new, 0, sizeof(*new));

  new->next=NULL; /* just in case NULL is not represented by a binary 0 */

  current->next=new;
  current=new;
}

/* set size of current element to at least size bytes */
void set_size(long size)
{
  long nalloc;

  if(current->allocated_size==0) {
    nalloc=size;
    if(nalloc<ALLOCSIZE) nalloc=ALLOCSIZE;
    current->data=xalloc(nalloc);
    current->allocated_size=nalloc;
  } else
  if(current->allocated_size<size) {
    nalloc=size-current->allocated_size;
    if(nalloc<ALLOCSIZE) nalloc=ALLOCSIZE;
    current->data=xrealloc(current->data, current->allocated_size+nalloc);
    current->allocated_size+=nalloc;
  }
}

void store_block(char *data, int size)
{
  set_size(current->size+size);
  memcpy(current->data+current->size, data, size);
  current->size+=size;
}

void allocate_image(void)
{
  allocate_element();
  current->GIFtype=GIFimage;
  current->imagestruct=xalloc(sizeof(*current->imagestruct));
  memset(current->imagestruct, 0, sizeof(*current->imagestruct));
}

void fix_current(void)
{
  if(current->allocated_size!=current->size) {
    current->data=xrealloc(current->data, current->size);
    current->allocated_size=current->size;
  }
}

byte *access_data(struct GIFelement *e, unsigned long pos, unsigned long len)
{
  return e->data+pos;
}

void free_mem(void)
{
  struct GIFelement *p,*t;

  p=first.next;
  first.next=NULL;

  while(p) {
    t=p;
    p=p->next;
    if(t->data) free(t->data);
    if(t->imagestruct) free(t->imagestruct);
    free(t);
  }
}
#else /* TMPFILE */

/* use temporary file for most of the data */

/* allocate a new GIFelement, advance current pointer and initialize
   size to 0 */

void allocate_element(void)
{
  struct GIFelement *new=xalloc(sizeof(*new));

  memset(new, 0, sizeof(*new));
  current->next=new;
  current=new;

  current->file_offset=ftell(tempfile);
}

void set_size(long size)
{
}

void store_block(char *data, int size)
{
  fwrite(data, 1, size, tempfile);
  current->size+=size;
}

void allocate_image(void)
{
  allocate_element();
  current->GIFtype=GIFimage;
  current->imagestruct=xalloc(sizeof(*current->imagestruct));
  memset(current->imagestruct, 0, sizeof(*current->imagestruct));
}

void fix_current(void)
{
}

byte *access_data(struct GIFelement *e, unsigned long pos, unsigned long len)
{
  static byte *data=NULL;
  static size_t allocated_size=0;

#ifdef PNG_MAX_ALLOC_64K
  if(len>65535) {
    fprintf(stderr, "single image element too large, use the 32 bit version of gif2png\n");
    exit(1);
  }
#endif

  if(len==0) {
    free(data);
    data=NULL;
    allocated_size=0;
    return NULL;
  }

  if(data==NULL) {
    data=xalloc(len);
    allocated_size=len;
  } else {
    if(allocated_size!=len) {
      data=xrealloc(data, len);
      allocated_size=len;
    }
  }

  fseek(tempfile, e->file_offset+pos, SEEK_SET);
  fread(data, 1, len, tempfile);

  return data;
}

void free_mem(void)
{
  struct GIFelement *p,*t;

  p=first.next;
  first.next=NULL;

  while(p) {
    t=p;
    p=p->next;
    if(t->imagestruct) free(t->imagestruct);
    free(t);
  }
  access_data(NULL, 0, 0);
}
#endif /* TMPFILE */