/[cvs]/eggdrop1.9/src/egglib/mstack.c
ViewVC logotype

Contents of /eggdrop1.9/src/egglib/mstack.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Sun Oct 28 13:30:35 2001 UTC (17 years, 11 months ago) by ite
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/x-chdr
FILE REMOVED
Renamed src/adns, src/compat, src/egglib to lib/adns, lib/compat, lib/egglib respectively.

1 /* Implement a stack based on malloc. */
2
3 #include <stdio.h>
4 #include "mstack.h"
5
6 mstack_t *mstack_new(int initial_size)
7 {
8 mstack_t *m;
9
10 if (initial_size <= 0) initial_size = 10;
11 m = (mstack_t *)malloc(sizeof(mstack_t) + sizeof(int) * initial_size);
12 m->len = 0;
13 m->max = initial_size;
14 m->stack = ((int *)m)+3;
15 return(m);
16 }
17
18 int mstack_destroy(mstack_t *m)
19 {
20 if (m->stack != ((int *)m)+3) free(m->stack);
21 free(m);
22 return(0);
23 }
24
25 void *mstack_push(mstack_t *m, void *item)
26 {
27 if (m->len == m->max) mstack_grow(m, 10);
28 m->stack[m->len] = (int) item;
29 m->len++;
30 return(item);
31 }
32
33 int mstack_pop(mstack_t *m, void **itemptr)
34 {
35 if (m->len == 0) return(1);
36 m->len--;
37 *itemptr = (void *)m->stack[m->len];
38 return(0);
39 }
40
41 int mstack_grow(mstack_t *m, int nsteps)
42 {
43 if (m->stack == ((int *)m)+3) {
44 int *newstack;
45
46 newstack = (int *)malloc(sizeof(int) * (m->max + nsteps));
47 memcpy(newstack, m->stack, sizeof(int) * m->max);
48 m->stack = newstack;
49 }
50 else m->stack = (int *)realloc(m->stack, sizeof(int) * (m->max + nsteps));
51
52 m->max += nsteps;
53 return(0);
54 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23