CoreFW  Check-in [8ec09e82ac]

Overview
Comment:Add refpool.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8ec09e82ac241bb8ed7de05fcc7a294d8a6a3319afde101f140854e305d6aeea
User & Date: js on 2012-04-22 13:09:52
Other Links: manifest | tags
Context
2012-04-22
13:18
Don't install tests. check-in: e5f3d00d30 user: js tags: trunk
13:09
Add refpool. check-in: 8ec09e82ac user: js tags: trunk
12:50
Add -Werror and -pipe if when using GCC. check-in: b18f80cc04 user: js tags: trunk
Changes

Modified src/Makefile from [93ec1092c3] to [5cb56e5531].

1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
SHARED_LIB = ${LIB_PREFIX}corefw${LIB_SUFFIX}
LIB_MAJOR = 0
LIB_MINOR = 0

SRCS = array.c	\
       class.c	\
       map.c	\
       object.c	\
       range.c	\

       string.c

INCLUDES = ${SRCS:.c=.h}	\
	   corefw.h		\
	   hash.h

include ../buildsys.mk




|
|
|
|
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SHARED_LIB = ${LIB_PREFIX}corefw${LIB_SUFFIX}
LIB_MAJOR = 0
LIB_MINOR = 0

SRCS = array.c		\
       class.c		\
       map.c		\
       object.c		\
       range.c		\
       refpool.c	\
       string.c

INCLUDES = ${SRCS:.c=.h}	\
	   corefw.h		\
	   hash.h

include ../buildsys.mk

Modified src/object.c from [997da41042] to [3d2cb3ed1b].

21
22
23
24
25
26
27

28
29

30
31
32
33
34
35
36
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>


#include "object.h"


void*
cfw_new(CFWClass *class, ...)
{
	CFWObject *obj;

	if ((obj = malloc(class->size)) == NULL)







>


>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

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

#include "object.h"
#include "refpool.h"

void*
cfw_new(CFWClass *class, ...)
{
	CFWObject *obj;

	if ((obj = malloc(class->size)) == NULL)
46
47
48
49
50
51
52

































53
54
55
56
57
58
59
		if (!class->ctor(obj, args)) {
			cfw_unref(obj);
			return NULL;
		}

		va_end(args);
	}


































	return obj;
}

void*
cfw_ref(void *ptr)
{







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







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
		if (!class->ctor(obj, args)) {
			cfw_unref(obj);
			return NULL;
		}

		va_end(args);
	}

	return obj;
}

void*
cfw_new_p(CFWClass *class, ...)
{
	CFWObject *obj;

	assert(class != cfw_refpool);

	if ((obj = malloc(class->size)) == NULL)
		return NULL;

	obj->cls = class;
	obj->ref_cnt = 1;

	if (class->ctor != NULL) {
		va_list args;
		va_start(args, class);

		if (!class->ctor(obj, args)) {
			cfw_unref(obj);
			return NULL;
		}

		va_end(args);
	}

	if (!cfw_refpool_add(obj)) {
		cfw_unref(obj);
		return NULL;
	}

	return obj;
}

void*
cfw_ref(void *ptr)
{

Modified src/object.h from [dc1cf1f6a1] to [07013e8446].

32
33
34
35
36
37
38

39
40
41
42
43
44
45
typedef struct CFWObject {
	CFWClass *cls;
	int ref_cnt;
} CFWObject;

extern CFWClass *cfw_object;
extern void* cfw_new(CFWClass*, ...);

extern void* cfw_ref(void*);
extern void cfw_unref(void*);
extern void cfw_free(void*);
extern bool cfw_equal(void*, void*);
extern uint32_t cfw_hash(void*);
extern void* cfw_copy(void*);








>







32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
typedef struct CFWObject {
	CFWClass *cls;
	int ref_cnt;
} CFWObject;

extern CFWClass *cfw_object;
extern void* cfw_new(CFWClass*, ...);
extern void* cfw_new_p(CFWClass*, ...);
extern void* cfw_ref(void*);
extern void cfw_unref(void*);
extern void cfw_free(void*);
extern bool cfw_equal(void*, void*);
extern uint32_t cfw_hash(void*);
extern void* cfw_copy(void*);

Added src/refpool.c version [e46baebb14].

Added src/refpool.h version [c6422a47ca].

Modified tests/tests.c from [0544ff8a52] to [a4028e5ce6].

23
24
25
26
27
28
29

30
31
32
33
34
35
36
37
38
39
40
41
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>

#include "object.h"

#include "string.h"
#include "array.h"
#include "map.h"

void
print_map(CFWMap *map)
{
	cfw_map_iter_t iter;

	cfw_map_iter(map, &iter);
	cfw_map_iter_next(&iter);








>




|







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>

#include "object.h"
#include "refpool.h"
#include "string.h"
#include "array.h"
#include "map.h"

static void
print_map(CFWMap *map)
{
	cfw_map_iter_t iter;

	cfw_map_iter(map, &iter);
	cfw_map_iter_next(&iter);

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

	fputs("}\n", stdout);
}

int
main()
{
	CFWString *s[3];
	CFWArray *a;

	CFWMap *m;
	size_t i;

	s[0] = cfw_new(cfw_string, "Hallo");
	s[1] = cfw_new(cfw_string, " Welt");
	s[2] = cfw_new(cfw_string, "!");

	a = cfw_new(cfw_array, s[0], s[1], s[2], NULL);

	cfw_unref(s[0]);
	cfw_unref(s[1]);
	cfw_unref(s[2]);

	s[0] = cfw_new(cfw_string, NULL);

	for (i = 0; i < cfw_array_size(a); i++)
		cfw_string_append(s[0], cfw_array_get(a, i));

	cfw_unref(a);

	puts(cfw_string_c(s[0]));


	s[1] = cfw_new(cfw_string, "ll");
	printf("%zd\n", cfw_string_find(s[0], s[1], cfw_range_all));
	cfw_unref(s[1]);

	cfw_unref(s[0]);


	s[0] = cfw_new(cfw_string, "Hallo");
	s[1] = cfw_new(cfw_string, "Welt!");

	m = cfw_new(cfw_map, s[0], s[1], NULL);
	cfw_unref(s[1]);




	print_map(m);

	s[1] = cfw_new(cfw_string, "Test");
	cfw_map_set(m, s[0], s[1]);
	cfw_unref(s[1]);


	print_map(m);

	cfw_map_set(m, s[0], NULL);
	print_map(m);

	cfw_unref(s[0]);
	cfw_unref(m);

	return 0;
}







|

>



<
<
|

|
|
|
|
<

|


|

|

|

>
|
|
<

|
>

|
<

|
|
>
>
>



<
|
|
>



|


|
<



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

	fputs("}\n", stdout);
}

int
main()
{
	CFWRefPool *p;
	CFWArray *a;
	CFWString *s, *s2;
	CFWMap *m;
	size_t i;



	p = cfw_new(cfw_refpool);

	a = cfw_new_p(cfw_array,
	    cfw_new_p(cfw_string, "Hallo"),
	    cfw_new_p(cfw_string, " Welt"),
	    cfw_new_p(cfw_string, "!"), NULL);


	s = cfw_new(cfw_string, NULL);

	for (i = 0; i < cfw_array_size(a); i++)
		cfw_string_append(s, cfw_array_get(a, i));

	cfw_unref(p);

	puts(cfw_string_c(s));

	p = cfw_new(cfw_refpool);
	s2 = cfw_new_p(cfw_string, "ll");
	printf("%zd\n", cfw_string_find(s, s2, cfw_range_all));


	cfw_unref(p);
	cfw_unref(s);

	p = cfw_new(cfw_refpool);


	m = cfw_new_p(cfw_map,
	    cfw_new_p(cfw_string, "Hallo"),
	    cfw_new_p(cfw_string, "Welt!"),
	    cfw_new_p(cfw_string, "Test"),
	    cfw_new_p(cfw_string, "success!"), NULL);

	print_map(m);


	cfw_map_set(m,
	    cfw_new_p(cfw_string, "Hallo"),
	    cfw_new_p(cfw_string, "Test"));

	print_map(m);

	cfw_map_set(m, cfw_new_p(cfw_string, "Hallo"), NULL);
	print_map(m);

	cfw_unref(p);


	return 0;
}