Skip to content

Commit 15b825f

Browse files
committed
Changed smalloc prototype to used named parameters
1 parent 74a7790 commit 15b825f

File tree

7 files changed

+43
-110
lines changed

7 files changed

+43
-110
lines changed

ChangeLog

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
2015-03-18 Franklin "Snaipe" Mathieu <[email protected]>
22

33
* include/csptr/{smart_ptr.h,apply.h}: Removed the destructor helper macro
4-
* include/csptr/smart_ptr.h: Changed the vararg usage to struct vararg
5-
This change is **NOT** backward-compatible. Parenthesis around the value
6-
were removed with this change, and metadata is no longer passed as two
7-
separate parameters.
4+
* include/csptr/{smart_ptr.h,smalloc.h}: Changed the vararg usage to
5+
struct vararg. This change is **NOT** backward-compatible. Parenthesis
6+
around the value were removed with this change, and metadata is no longer
7+
passed as two separate parameters.
88

99
2015-01-26 Franklin "Snaipe" Mathieu <[email protected]>
1010

Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ EXTRA_DIST = LICENSE README.md
1919

2020
subdirincludedir = $(includedir)/csptr/
2121
subdirinclude_HEADERS = \
22-
include/csptr/vararg.h \
2322
include/csptr/smalloc.h \
2423
include/csptr/array.h \
2524
include/csptr/smart_ptr.h

check/test/misc.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
#include "config.h"
44
#include "utils.h"
55

6-
START_TEST (test_more_args) {
7-
smart void *ptr = smalloc(sizeof (int), 0, UNIQUE, NULL, NULL, NULL, NULL);
8-
assert_valid_ptr(ptr);
9-
} END_TEST
10-
116
START_TEST (test_zero_size) {
127
void *ptr = smalloc(0, 0, UNIQUE);
138
ck_assert_msg(ptr == NULL, "Expected NULL pointer to be returned.");
@@ -26,7 +21,6 @@ START_TEST (test_alloc_failure) {
2621
#endif
2722

2823
TFun misc_tests[] = {
29-
test_more_args,
3024
#ifndef SMALLOC_FIXED_ALLOCATOR
3125
test_alloc_failure,
3226
#endif

include/csptr/smalloc.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
# define CSPTR_SMALLOC_H_
2727

2828
# include <stdlib.h>
29-
# include "vararg.h"
3029

3130
enum pointer_kind {
3231
UNIQUE,
@@ -44,13 +43,26 @@ typedef struct {
4443

4544
extern s_allocator smalloc_allocator;
4645

46+
typedef struct {
47+
int sentinel_;
48+
size_t size;
49+
size_t nmemb;
50+
enum pointer_kind kind;
51+
f_destructor dtor;
52+
struct {
53+
const void *data;
54+
size_t size;
55+
} meta;
56+
} s_smalloc_args;
57+
4758
__attribute__ ((pure))
4859
void *get_smart_ptr_meta(void *ptr);
4960
void *sref(void *ptr);
5061
__attribute__((malloc))
51-
void *smalloc(size_t size, size_t nmemb, int kind, int count, ...);
62+
void *smalloc(s_smalloc_args *args);
5263
void sfree(void *ptr);
5364

54-
# define smalloc(Size, Nmemb, Kind, Args...) smalloc(Size, Nmemb, Kind, ARG_LENGTH(Args), ## Args)
65+
# define smalloc(...) \
66+
smalloc(&(s_smalloc_args) { .sentinel_ = 0, __VA_ARGS__ })
5567

5668
#endif /* !CSPTR_SMALLOC_H_ */

include/csptr/smart_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ inline void sfree_stack(void *ptr) {
3535
*real_ptr = NULL;
3636
}
3737

38-
# define ARGS_ args.dtor, args.meta.ptr, args.meta.size
38+
# define ARGS_ args.dtor, { args.meta.ptr, args.meta.size }
3939

4040
# define smart __attribute__ ((cleanup(sfree_stack)))
4141
# define smart_ptr(Kind, Type, Args...) \

include/csptr/vararg.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/mman.c

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -87,85 +87,60 @@ INLINE static void dealloc_entry(s_meta *meta, void *ptr) {
8787
}
8888

8989
__attribute__ ((malloc))
90-
static void *smalloc_impl(size_t size, int kind, f_destructor dtor, void *meta, size_t metasize) {
91-
if (!size)
90+
static void *smalloc_impl(s_smalloc_args *args) {
91+
if (!args->size)
9292
return NULL;
9393

9494
// align the sizes to the size of a word
95-
size_t aligned_metasize = align(metasize);
96-
size = align(size);
95+
size_t aligned_metasize = align(args->meta.size);
96+
size_t size = align(args->size);
9797

98-
size_t head_size = kind & SHARED ? sizeof (s_meta_shared) : sizeof (s_meta);
98+
size_t head_size = args->kind & SHARED ? sizeof (s_meta_shared) : sizeof (s_meta);
9999
s_meta_shared *ptr = alloc_entry(head_size, size, aligned_metasize);
100100
if (ptr == NULL)
101101
return NULL;
102102

103103
char *shifted = (char *) ptr + head_size;
104-
if (metasize && meta)
105-
memcpy(shifted, meta, metasize);
104+
if (args->meta.size && args->meta.data)
105+
memcpy(shifted, args->meta.data, args->meta.size);
106106

107107
size_t *sz = (size_t *) (shifted + aligned_metasize);
108108
*sz = head_size + aligned_metasize;
109109

110110
*(s_meta*) ptr = (s_meta) {
111-
.kind = kind,
112-
.dtor = dtor,
111+
.kind = args->kind,
112+
.dtor = args->dtor,
113113
#ifndef NDEBUG
114114
.ptr = sz + 1
115115
#endif
116116
};
117117

118-
if (kind & SHARED)
118+
if (args->kind & SHARED)
119119
ptr->ref_count = ATOMIC_VAR_INIT(1);
120120

121121
return sz + 1;
122122
}
123123

124124
__attribute__ ((malloc))
125-
INLINE static void *smalloc_array(size_t size, size_t nmemb, int kind, f_destructor dtor, void *meta, size_t metasize) {
126-
char new_meta[align(metasize + sizeof(s_meta_array))];
125+
INLINE static void *smalloc_array(s_smalloc_args *args) {
126+
char new_meta[align(args->meta.size + sizeof(s_meta_array))];
127127
s_meta_array *arr_meta = (void *) new_meta;
128128
*arr_meta = (s_meta_array) {
129-
.size = size,
130-
.nmemb = nmemb
129+
.size = args->size,
130+
.nmemb = args->nmemb,
131131
};
132-
memcpy(arr_meta + 1, meta, metasize);
133-
return smalloc_impl(nmemb * size, kind | ARRAY, dtor, &new_meta, sizeof (new_meta));
132+
memcpy(arr_meta + 1, args->meta.data, args->meta.size);
133+
return smalloc_impl(&(s_smalloc_args) {
134+
.size = args->nmemb * args->size,
135+
.kind = args->kind | ARRAY,
136+
.dtor = args->dtor,
137+
.meta = { &new_meta, sizeof (new_meta) },
138+
});
134139
}
135140

136141
__attribute__ ((malloc))
137-
void *smalloc(size_t size, size_t nmemb, int kind, int count, ...) {
138-
va_list args;
139-
140-
int params = 0;
141-
if (count == 2) {
142-
++count;
143-
++params;
144-
}
145-
if (count > 3)
146-
count = 3;
147-
148-
va_start(args, count);
149-
150-
struct {
151-
f_destructor dtor;
152-
void *meta_ptr;
153-
size_t meta_size;
154-
} values = { NULL, NULL, 0 };
155-
156-
while (params < count) {
157-
switch (params++) {
158-
case 0: values.dtor = va_arg(args, f_destructor); break;
159-
case 1: values.meta_ptr = va_arg(args, void *); break;
160-
case 2: values.meta_size = va_arg(args, size_t); break;
161-
}
162-
}
163-
164-
va_end(args);
165-
if (nmemb == 0)
166-
return smalloc_impl(size, kind, values.dtor, values.meta_ptr, values.meta_size);
167-
else
168-
return smalloc_array(size, nmemb, kind, values.dtor, values.meta_ptr, values.meta_size);
142+
void *smalloc(s_smalloc_args *args) {
143+
return (args->nmemb == 0 ? smalloc_impl : smalloc_array)(args);
169144
}
170145

171146
void sfree(void *ptr) {

0 commit comments

Comments
 (0)