File: fduserdata.c

package info (click to toggle)
libfduserdata 0.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 144 kB
  • sloc: ansic: 208; makefile: 17
file content (225 lines) | stat: -rw-r--r-- 6,262 bytes parent folder | download | duplicates (2)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 *   Associate user defined data to file descriptors. (thread-safe).
 *
 *   Copyright (C) 2019  Renzo Davoli <renzo@cs.unibo.it> VirtualSquare team.
 *
 *   This library is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation; either version 2.1 of the License, or (at
 *   your option) any later version.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software Foundation,
 *   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <pthread.h>
#include <fduserdata.h>

#define DEFAULT_MASK 0x3f // 63

#ifndef container_of
#define container_of(ptr, type, member) \
	((type *)((char *)(ptr) -  offsetof(type,member)))
#endif

struct fduserdata_item {
	struct fduserdata_item *next;
	struct fduserdata_table *fdtable;
	int fd;
	char data[];
};

struct fduserdata_table {
	int mask;
	pthread_mutex_t mutex;
	struct fduserdata_item **table;
};

static int size2mask(int x) {
	int scan;
	x = x - 1;
	if (x < 0)
		return DEFAULT_MASK;
	else {
		for (scan = -1; scan & x; scan <<= 1)
			;
		return ~scan;
	}
}

FDUSERDATA *fduserdata_create(int size) {
	int mask = size2mask(size);
	struct fduserdata_table *fdtable = malloc(sizeof(struct fduserdata_table));
	if (fdtable != NULL) {
		fdtable->mask = mask;
		pthread_mutex_init(&fdtable->mutex, NULL);
		fdtable->table = NULL;
	}
	return fdtable;
}

void fduserdata_destroy_cb(FDUSERDATA *fdtable, fduserdata_destr_cb_t callback, void *arg) {
	if (fdtable != NULL) {
		pthread_mutex_lock(&fdtable->mutex);
		if (fdtable->table != NULL) {
			int i;
			for (i = 0; i < (fdtable->mask + 1) ; i++) {
				struct fduserdata_item *fdud = fdtable->table[i];
				while (fdud != NULL) {
					struct fduserdata_item *this = fdud;
					if (callback)
						callback(fdud->fd, fdud->data, arg);
					fdud = this->next;
					free(this);
				}
			}
			free(fdtable->table);
		}
		pthread_mutex_unlock(&fdtable->mutex);
		pthread_mutex_destroy(&fdtable->mutex);
		free(fdtable);
	}
}

void fduserdata_destroy(FDUSERDATA *fdtable) {
	fduserdata_destroy_cb(fdtable, NULL, NULL);
}

void *__fduserdata_new(FDUSERDATA *fdtable, int fd, size_t count) {
	if (fdtable != NULL) {
		struct fduserdata_item *fdud = malloc(sizeof(struct fduserdata_item) + count);
		int index;
		if (fdud == NULL)
			return errno = ENOMEM, NULL;
		pthread_mutex_lock(&fdtable->mutex);
		if (fdtable->table == NULL) {
			fdtable->table = calloc(fdtable->mask + 1, sizeof(struct fduserdata_item *));
			if (fdtable->table == NULL) {
				free(fdud);
				pthread_mutex_unlock(&fdtable->mutex);
				return errno = ENOMEM, NULL;
			}
		}
		index = fd & fdtable->mask;
		fdud->fd = fd;
		fdud->fdtable = fdtable;
		fdud->next = fdtable->table[index];
		fdtable->table[index] = fdud;
		return fdud->data;
	} else
		return errno = EINVAL, NULL;
}

void *fduserdata_get(FDUSERDATA *fdtable, int fd) {
	if (fdtable != NULL) {
		pthread_mutex_lock(&fdtable->mutex);
		int index = fd & fdtable->mask;
		struct fduserdata_item *fdud;
		if (fdtable->table == NULL)
			fdud = NULL;
		else {
			for (fdud = fdtable->table[index]; fdud != NULL; fdud = fdud->next)
				if (fdud->fd == fd) break;
		}
		if (fdud == NULL) {
			pthread_mutex_unlock(&fdtable->mutex);
			return errno = EBADF, NULL;
		} else
			return fdud->data;
	} else
		return errno = EINVAL, NULL;
}


static inline __attribute__((always_inline)) struct fduserdata_item *data2item(void *data) {
	return container_of(data, struct fduserdata_item, data);
}

void fduserdata_put(void *data) {
	if (data != NULL) {
		struct fduserdata_item *fdud = data2item(data);
		pthread_mutex_unlock(&fdud->fdtable->mutex);
	}
}

int fduserdata_del(void *data) {
	if (data == NULL)
		return errno = EINVAL, -1;
	else {
		struct fduserdata_item *fdud = data2item(data);
		FDUSERDATA *fdtable = fdud->fdtable;
		int fd = fdud->fd;
		int index = fd & fdtable->mask;
		struct fduserdata_item **sfdud;
		for (sfdud = &(fdtable->table[index]); *sfdud != NULL && (*sfdud) != fdud; sfdud = &((*sfdud)->next))
			;
		if (*sfdud != NULL) {
			*sfdud = fdud->next;
			free(fdud);
			pthread_mutex_unlock(&fdtable->mutex);
			return 0;
		} else {
			pthread_mutex_unlock(&fdtable->mutex);
			return errno = EBADF, -1;
		}
	}
}

#if 0
void destructor(int fd, void *data, void *arg) {
	printf("%d %d\n", fd, *(int *)data);
}

int main(int argc, char *argv[]) {
	FDUSERDATA *fdtable = fduserdata_create(0);
	printf("XXX %d\n", sizeof(struct fduserdata_item));
	int *data;
	data = __fduserdata_new(fdtable, 1, sizeof(* data));
	if (data) *data = 1;
	if (data) fduserdata_put(data);
	data = __fduserdata_new(fdtable, 2, sizeof(* data));
	if (data) if (data) *data = 2;
	fduserdata_put(data);
	data = __fduserdata_new(fdtable, 65, sizeof(* data));
	if (data) if (data) *data = 65;
	fduserdata_put(data);
	data = fduserdata_get(fdtable, 1);
	if (data) printf("%d\n",*data);
	if (data) fduserdata_put(data);
	data = fduserdata_get(fdtable, 2);
	if (data) printf("%d\n",*data);
	if (data) fduserdata_put(data);
	data = fduserdata_get(fdtable, 65);
	if (data) printf("%d\n",*data);
	if (data) fduserdata_put(data);
	data = fduserdata_get(fdtable, 2);
	if (data) fduserdata_del(data);
	data = fduserdata_get(fdtable, 3);
	if (data) fduserdata_del(data);
	data = fduserdata_get(fdtable, 2);
	if (data) printf("%d\n",*data); else printf("NULL\n");
	if (data) fduserdata_put(data);
	data = fduserdata_get(fdtable, 1);
	if (data) printf("%d\n",*data); else printf("NULL\n");
	if (data) fduserdata_put(data);
	data = fduserdata_get(fdtable, 1);
	if (data) fduserdata_del(data);
	data = fduserdata_get(fdtable, 1);
	if (data) printf("%d\n",*data); else printf("NULL\n");
	if (data) fduserdata_put(data);
	data = fduserdata_get(fdtable, 65);
	if (data) printf("%d\n",*data); else printf("NULL\n");
	if (data) fduserdata_put(data);
	data = __fduserdata_new(fdtable, 44, sizeof(* data));
	if (data) if (data) *data = 44;
	fduserdata_put(data);
	//fduserdata_destroy(fdtable);
	fduserdata_destroy_cb(fdtable, destructor, NULL);
}
#endif