ObjFW  Check-in [7b8b7cd06c]

Overview
Comment:Lots of changes. See full commit message.

* Updated buildsys to fixed version.
* Implement exceptions.
* Let OFObject use exceptions.
* Write tests for OFObject.
* Fix a bug in OFObject's freeMem:.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7b8b7cd06c0185266186b9e1304cfd13300ed9f60b884b3346dfa93d0ec09cf1
User & Date: js on 2008-09-14 16:43:54
Other Links: manifest | tags
Context
2008-09-14
17:09
Automatically run tests. check-in: ab91040318 user: js tags: trunk
16:43
Lots of changes. See full commit message. check-in: 7b8b7cd06c user: js tags: trunk
15:29
Coding style. check-in: 778be56179 user: js tags: trunk
Changes

Modified buildsys.mk.in from [3204adcbc4] to [be3d55644e].

97
98
99
100
101
102
103



104

105
106








107
108
109
110
111
112
113

depend: pre-depend ${SRCS}
	regen=0; \
	deps=""; \
	test -f .deps || regen=1; \
	for i in ${SRCS}; do \
		case $$i in \



			*.o) \

				test $$i -nt .deps && regen=1; \
				deps="$${deps%.o}.dep $$i"; \








				;; \
		esac; \
	done; \
	if test x"$$regen" = x"1" -a x"$$deps" != "x"; then \
		${DEPEND_STATUS}; \
		rm -f .deps; \
		if ${MAKE} ${MFLAGS} $$deps; then \







>
>
>
|
>

|
>
>
>
>
>
>
>
>







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

depend: pre-depend ${SRCS}
	regen=0; \
	deps=""; \
	test -f .deps || regen=1; \
	for i in ${SRCS}; do \
		case $$i in \
			*.c) \
				test $$i -nt .deps && regen=1; \
				deps="$$deps $${i%.c}.dep"; \
				;; \
			*.cc) \
				test $$i -nt .deps && regen=1; \
				deps="$$deps $${i%.cc}.dep"; \
				;; \
			*.cxx) \
				test $$i -nt .deps && regen=1; \
				deps="$$deps $${i%.cxx}.dep"; \
				;; \
			*.m) \
				test $$i -nt .deps && regen=1; \
				deps="$$deps $${i%.m}.dep"; \
				;; \
		esac; \
	done; \
	if test x"$$regen" = x"1" -a x"$$deps" != "x"; then \
		${DEPEND_STATUS}; \
		rm -f .deps; \
		if ${MAKE} ${MFLAGS} $$deps; then \

Modified src/Makefile from [4590acbe8d] to [3c78222fcb].

1
2
3
4
5
6

7
8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX}
LIB_MAJOR = 1
LIB_MINOR = 0

SRCS = OFConstString.m		\
       OFConstWideString.m	\

       OFList.m			\
       OFListObject.m		\
       OFObject.m		\
       OFString.m		\
       OFWideString.m

OBJCFLAGS += -fPIC -DPIC -fno-nil-recivers -fconstant-string-class=OFConstString
INCLUDES = OFConstString.h	\
	   OFConstWideString.h	\

	   OFList.h		\
	   OFListObject.h	\
	   OFObject.h		\
	   OFString.h		\
	   OFWideString.h

include ../buildsys.mk






>






|


>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX}
LIB_MAJOR = 1
LIB_MINOR = 0

SRCS = OFConstString.m		\
       OFConstWideString.m	\
       OFException.m		\
       OFList.m			\
       OFListObject.m		\
       OFObject.m		\
       OFString.m		\
       OFWideString.m

OBJCFLAGS += -fPIC -DPIC -fno-nil-recivers -fconstant-string-class=OFConstString -fobjc-exceptions
INCLUDES = OFConstString.h	\
	   OFConstWideString.h	\
	   OFException.h	\
	   OFList.h		\
	   OFListObject.h	\
	   OFObject.h		\
	   OFString.h		\
	   OFWideString.h

include ../buildsys.mk

Added src/OFException.h version [aab3ba4a15].

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#define __NO_OFEXCEPTION
#import "OFObject.h"

@interface OFException: OFObject
@end

Added src/OFException.m version [6e35154e42].































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFException.h"

@implementation OFException
@end

Modified src/OFObject.h from [b47bfb1d54] to [55682be82c].

25
26
27
28
29
30
31











- init;
- (void*)getMem: (size_t)size;
- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size;
- (void)freeMem: (void*)ptr;
- free;
@end


















>
>
>
>
>
>
>
>
>
>
>
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
- init;
- (void*)getMem: (size_t)size;
- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size;
- (void)freeMem: (void*)ptr;
- free;
@end

#ifndef __NO_OFEXCEPTION
#import "OFException.h"

@interface OFMemNotPartOfObjException: OFException
+        new: (void*)p
  fromObject: (id)obj;
-       init: (void*)p
  fromObject: (id)obj;
@end
#endif

Modified src/OFObject.m from [b460fe3dcf] to [9572084d4f].

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



















				return NULL;
			
			iter->ptr = ptr;
			return ptr;
		}
	}

	fprintf(stderr, "WARNING: Memory at %p was not allocated as part of "
	    "object %s!\n-> Memory was not resized!\n", ptr, [self name]);

	return NULL;
}

- (void)freeMem: (void*)ptr;
{
	struct __ofobject_allocated_mem *iter;

	for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
		if (iter->ptr == ptr) {
			if (iter->prev != NULL)
				iter->prev->next = iter->next;
			if (iter->next != NULL)
				iter->next->prev = iter->prev;



			free(iter);
			free(ptr);

			return;
		}
	}

	fprintf(stderr, "WARNING: Memory at %p was not allocated as part of "
	    "object %s!\n-> Memory was not free'd!\n", ptr, [self name]);
}

- free
{
	struct __ofobject_allocated_mem *iter, *iter2;

	for (iter = __mem_pool; iter != NULL; iter = iter2) {
		iter2 = iter->prev;
		free(iter->ptr);
		free(iter);
	}

	return [super free];
}
@end


























<
<
|









|



>
>








|
<















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
				return NULL;
			
			iter->ptr = ptr;
			return ptr;
		}
	}



	@throw [OFMemNotPartOfObjException new: ptr fromObject: self];
	return NULL;
}

- (void)freeMem: (void*)ptr;
{
	struct __ofobject_allocated_mem *iter;

	for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
		if (iter->ptr == ptr) {
			if (iter->prev != NULL) 
				iter->prev->next = iter->next;
			if (iter->next != NULL)
				iter->next->prev = iter->prev;
			if (__mem_pool == iter)
				__mem_pool = NULL;

			free(iter);
			free(ptr);

			return;
		}
	}

	@throw [OFMemNotPartOfObjException new: ptr fromObject: self];

}

- free
{
	struct __ofobject_allocated_mem *iter, *iter2;

	for (iter = __mem_pool; iter != NULL; iter = iter2) {
		iter2 = iter->prev;
		free(iter->ptr);
		free(iter);
	}

	return [super free];
}
@end

@implementation OFMemNotPartOfObjException
+ new: (void*)ptr fromObject: (id)obj
{
	return [[OFMemNotPartOfObjException alloc] init: ptr
					     fromObject: obj];
}

- init: (void*)ptr fromObject: (id)obj
{
	fprintf(stderr, "ERROR: Memory at %p was not allocated as part of "
	    "object %s!\n"
	    "ERROR: -> Not changing memory allocation!\n"
	    "ERROR: (Hint: It is possible that you tried to free the same "
	    "memory twice!)\n", ptr, [obj name]);

	return [super init];
}
@end

Modified tests/Makefile from [40930cdf32] to [5187c45877].

1
2
3
SUBDIRS = OFString OFList OFWideString

include ../buildsys.mk
|


1
2
3
SUBDIRS = OFObject OFString OFList OFWideString

include ../buildsys.mk

Added tests/OFObject/Makefile version [cea45375f0].















>
>
>
>
>
>
>
1
2
3
4
5
6
7
PROG_NOINST = ofobject
SRCS = OFObject.m

include ../../buildsys.mk

CPPFLAGS += -I../../src
LIBS += -lobjc -L../../src -lobjfw

Added tests/OFObject/OFObject.m version [2c9d4130c6].













































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

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

#import "OFObject.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;
}