File: memory.c

package info (click to toggle)
gif2png 2.5.8-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 880 kB
  • ctags: 428
  • sloc: ansic: 2,958; sh: 1,929; xml: 836; python: 248; makefile: 85
file content (128 lines) | stat: -rw-r--r-- 2,651 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
/*
 * memory.c: memory storage for gif conversion
 * Copyright (C) 1995 Alexander Lehmann
 * For conditions of distribution and use, see the file COPYING.
 */

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

#include "gif2png.h"

/*@-compdef@*/
void *xalloc(size_t s)
{
    void *p=malloc(s);

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

    return p;
}
/*@=compdef@*/

/*@-temptrans@*/
void *xrealloc(void *p, size_t s)
{
    p=realloc(p,s);

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

    return p;
}
/*@=temptrans@*/

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

#define ALLOCSIZE 8192

/*@-onlytrans -nullstate -mustfreeonly@*/
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;
}
/*@=onlytrans =nullstate =mustfreeonly@*/

/*@-mustfreeonly@*/
/* set size of current element to at least size bytes */
void set_size(size_t size)
{
    size_t 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;
	}
}
/*@=mustfreeonly@*/

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

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

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

/*@-nullstate@*/
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);
    }
}
/*@=nullstate@*/