ObjFW  Diff

Differences From Artifact [b4bf13ad01]:

To Artifact [201b5d89a5]:


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

#import <stdio.h>
#import <stdlib.h>
#import <stdbool.h>

#import "OFObject.h"
#import "OFExceptions.h"














int
main()
{
	OFObject *obj = [OFObject new];
	bool caught;
	void *p, *q, *r;

	/* Test freeing memory not allocated by obj */
	puts("Freeing memory not allocated by object (should throw an "
	    "exception)...");
	caught = false;
	@try {
		[obj freeMem: (void*)123];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}

	/* Test allocating memory */
	puts("Allocating memory through object...");
	p = [obj getMem: 4096];
	puts("Allocated 4096 bytes.");

	/* Test freeing the just allocated memory */
	puts("Freeing just allocated memory...");
	[obj freeMem: p];
	puts("Free'd.");

	/* It shouldn't be recognized as part of our obj anymore */
	puts("Trying to free it again (should throw an exception)...");
	caught = false;
	@try {
		[obj freeMem: p];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}

	/* Test multiple memory chunks */
	puts("Allocating 3 chunks of memory...");
	p = [obj getMem: 4096];
	q = [obj getMem: 4096];
	r = [obj getMem: 4096];
	puts("Allocated 3 * 4096 bytes.");

	/* Free them */
	puts("Now freeing them...");
	[obj freeMem: p];
	[obj freeMem: q];
	[obj freeMem: r];
	puts("Freed them all.");

	/* Try to free again */
	puts("Now trying to free them again...");
	caught = false;
	@try {
		[obj freeMem: p];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}
	caught = false;
	@try {
		[obj freeMem: q];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}
	caught = false;
	@try {
		[obj freeMem: r];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}
	puts("Got all 3!");

	
	/* TODO: Test if freeing object frees all memory */

	return 0;
}







>
>
>
>
>
>
>
>
>
>
>
>
>











<
<
|
<
<
<
<
<
<
<
<













<
<
<
|
<
<
<
<
<
<
<

















<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
|
<
|
|
>





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

#import <stdio.h>
#import <stdlib.h>
#import <stdbool.h>

#import "OFObject.h"
#import "OFExceptions.h"

#define CATCH_EXCEPTION(code, exception)	\
	caught = false;				\
	@try {					\
		code;				\
	} @catch (exception *e) {		\
		caught = true;			\
		puts("CAUGHT! Resuming...");	\
	}					\
	if (!caught) {				\
		puts("NOT CAUGHT!");		\
		return 1;			\
	}

int
main()
{
	OFObject *obj = [OFObject new];
	bool caught;
	void *p, *q, *r;

	/* Test freeing memory not allocated by obj */
	puts("Freeing memory not allocated by object (should throw an "
	    "exception)...");


	CATCH_EXCEPTION([obj freeMem: (void*)123], OFMemNotPartOfObjException)









	/* Test allocating memory */
	puts("Allocating memory through object...");
	p = [obj getMem: 4096];
	puts("Allocated 4096 bytes.");

	/* Test freeing the just allocated memory */
	puts("Freeing just allocated memory...");
	[obj freeMem: p];
	puts("Free'd.");

	/* It shouldn't be recognized as part of our obj anymore */
	puts("Trying to free it again (should throw an exception)...");



	CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException)








	/* Test multiple memory chunks */
	puts("Allocating 3 chunks of memory...");
	p = [obj getMem: 4096];
	q = [obj getMem: 4096];
	r = [obj getMem: 4096];
	puts("Allocated 3 * 4096 bytes.");

	/* Free them */
	puts("Now freeing them...");
	[obj freeMem: p];
	[obj freeMem: q];
	[obj freeMem: r];
	puts("Freed them all.");

	/* Try to free again */
	puts("Now trying to free them again...");



	CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException)










	CATCH_EXCEPTION([obj freeMem: q], OFMemNotPartOfObjException)










	CATCH_EXCEPTION([obj freeMem: r], OFMemNotPartOfObjException)




	puts("Got all 3!");

	
	puts("Trying to allocate more memory than possible...");
	CATCH_EXCEPTION(p = [obj getMem: 4294967295U], OFNoMemException)
	
	/* TODO: Test if freeing object frees all memory */

	return 0;
}