Merge from vendor branch NTPD:
[dragonfly.git] / sys / bus / cam / cam_extend.c
1 /*
2  * Written by Julian Elischer (julian@tfs.com)
3  * for TRW Financial Systems for use under the MACH(2.5) operating system.
4  *
5  * TRW Financial Systems, in accordance with their agreement with Carnegie
6  * Mellon University, makes this software available to CMU to distribute
7  * or use in any manner that they see fit as long as this message is kept with
8  * the software. For this reason TFS also grants any other persons or
9  * organisations permission to use or modify this software.
10  *
11  * TFS supplies this software to be publicly redistributed
12  * on the understanding that TFS is not responsible for the correct
13  * functioning of this software in any circumstances.
14  *
15  * $FreeBSD: src/sys/cam/cam_extend.c,v 1.3 1999/08/28 00:40:39 peter Exp $
16  * $DragonFly: src/sys/bus/cam/cam_extend.c,v 1.4 2004/03/12 03:23:13 dillon Exp $
17  */
18 /*
19  * XXX XXX XXX XXX  We should get DEVFS working so that we
20  * don't have to do this, possibly sparse, array based junk.
21  * XXX: We can do this now with dev_t, that's even better.
22  */
23 /*
24  * Extensible arrays: Use a realloc like implementation to permit
25  * the arrays to be extend.
26  */
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
31
32 #include "cam_extend.h"
33
34 struct extend_array
35 {
36         int nelem;
37         void **ps;
38 };
39
40 static void *
41 cam_extend_alloc(size_t s)
42 {
43         return(malloc(s, M_DEVBUF, M_INTWAIT));
44 }
45
46 static void
47 cam_extend_free(void *p)
48 {
49         free(p, M_DEVBUF);
50 }
51
52 /* EXTEND_CHUNK: Number of extend slots to allocate whenever we need a new
53  * one.
54  */
55 #ifndef EXTEND_CHUNK
56         #define EXTEND_CHUNK 8
57 #endif
58
59 struct extend_array *
60 cam_extend_new(void)
61 {
62         struct extend_array *p = cam_extend_alloc(sizeof(*p));
63         if (p) {
64                 p->nelem = 0;
65                 p->ps = 0;
66         }
67
68         return p;
69 }
70
71 void *
72 cam_extend_set(struct extend_array *ea, int index, void *value)
73 {
74         if (index >= ea->nelem) {
75                 void **space;
76                 space = cam_extend_alloc(sizeof(void *) * (index + EXTEND_CHUNK));
77                 bzero(space, sizeof(void *) * (index + EXTEND_CHUNK));
78
79                 /* Make sure we have something to copy before we copy it */
80                 if (ea->nelem) {
81                         bcopy(ea->ps, space, sizeof(void *) * ea->nelem);
82                         cam_extend_free(ea->ps);
83                 }
84
85                 ea->ps = space;
86                 ea->nelem = index + EXTEND_CHUNK;
87         }
88         if (ea->ps[index]) {
89                 printf("extend_set: entry %d already has storage.\n", index);
90                 return 0;
91         }
92         else
93                 ea->ps[index] = value;
94
95         return value;
96 }
97
98 void *
99 cam_extend_get(struct extend_array *ea,
100                int index)
101 {
102         if (ea == NULL || index >= ea->nelem || index < 0)
103                 return NULL;
104         return ea->ps[index];
105 }
106
107 void
108 cam_extend_release(struct extend_array *ea, int index)
109 {
110         void *p = cam_extend_get(ea, index);
111         if (p) {
112                 ea->ps[index] = 0;
113         }
114 }