File: mem.C

package info (click to toggle)
librudiments0 0.27-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,528 kB
  • ctags: 2,284
  • sloc: cpp: 14,657; sh: 7,547; ansic: 2,664; makefile: 945; xml: 15
file content (105 lines) | stat: -rw-r--r-- 2,102 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
// Copyright (c) 2002  David Muse
// See the file COPYING for more information

#include <rudiments/memorypool.h>
#include <stdio.h>

int main(int argc, const char **argv) {

	memorypool	*mp=new memorypool(32,16,10);

	printf("first run...\n");
	for (int i=0; i<20; i++) {
		char	*segment=(char *)mp->malloc(6);
		for (int j=0; j<6; j++) {
			segment[j]=(char)'a'+i;
		}
	}
	mp->print();

	printf("free...\n");
	mp->free();
	mp->print();

	printf("second run...\n");
	char	*segment=(char *)mp->malloc(40);
	for (int j=0; j<40; j++) {
		segment[j]='z';
	}
	for (int i=0; i<20; i++) {
		char	*segment=(char *)mp->malloc(6);
		for (int j=0; j<6; j++) {
			segment[j]=(char)'a'+i;
		}
	}
	for (int i=0; i<20; i++) {
		char	*segment=(char *)mp->malloc(2);
		for (int j=0; j<6; j++) {
			segment[j]=(char)'A'+i;
		}
	}
	mp->print();

	printf("free...\n");
	mp->free();
	mp->print();

	printf("short/long/float/double...\n");

	short	*sp=(short *)mp->malloc(sizeof(short));
	long	*lp=(long *)mp->malloc(sizeof(long));
	float	*fp=(float *)mp->malloc(sizeof(float));
	double	*dp=(double *)mp->malloc(sizeof(double));
	*sp=1;
	*lp=1;
	*fp=1.1;
	*dp=1.1;

	printf("sp: %d  lp: %ld ",*sp,*lp);
	printf("fp: %f  dp: %f \n",*fp,*dp);
	mp->print();

	printf("free...\n");
	mp->free();
	mp->print();

	printf("short/long/float/double arrays...\n");

	short	*spa=(short *)mp->malloc(sizeof(short)*10);
	long	*lpa=(long *)mp->malloc(sizeof(long)*10);
	float	*fpa=(float *)mp->malloc(sizeof(float)*10);
	double	*dpa=(double *)mp->malloc(sizeof(double)*10);
	for (int i=0; i<10; i++) {
		spa[i]=i;
		lpa[i]=i;
		fpa[i]=i+((float)i/10.0);
		dpa[i]=i+((float)i/10.0);
	}

	for (int i=0; i<10; i++) {
		printf("sp: %d  lp: %ld ",spa[i],lpa[i]);
		printf("fp: %f  dp: %f \n",fpa[i],dpa[i]);
	}
	mp->print();

	printf("free...\n");
	mp->free();
	mp->print();

	delete mp;


	mp=new memorypool(32,16,10);
	for (int i=0; i<10; i++) {
		unsigned long	total=0;
		for (unsigned long j=0; j<11; j++) {
			total=total+i+j;
			mp->malloc(i+j);
			mp->free();
		}
		printf("should be: %ld\n",total/11);
		mp->print();
	}

	delete mp;
}