I should drink more coffee before starting committing. Revert the last change
[dragonfly.git] / sys / bus / cam / cam_extend.c
blob2ef566e7b18de8b33fbcf5bdcd4a34c2d7267166
1 /*
2 * Written by Julian Elischer (julian@tfs.com)
3 * for TRW Financial Systems for use under the MACH(2.5) operating system.
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.
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.
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.8 2006/12/22 23:12:16 swildner Exp $
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 cdev_t, that's even better.
24 * Extensible arrays: Use a realloc like implementation to permit
25 * the arrays to be extend.
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
32 #include "cam_extend.h"
34 struct extend_array
36 int nelem;
37 void **ps;
40 /* EXTEND_CHUNK: Number of extend slots to allocate whenever we need a new
41 * one.
43 #ifndef EXTEND_CHUNK
44 #define EXTEND_CHUNK 8
45 #endif
47 struct extend_array *
48 cam_extend_new(void)
50 return(kmalloc(sizeof(struct extend_array), M_DEVBUF,
51 M_INTWAIT | M_ZERO));
54 void *
55 cam_extend_set(struct extend_array *ea, int index, void *value)
57 if (index >= ea->nelem) {
58 void **space;
59 space = kmalloc(sizeof(void *) * (index + EXTEND_CHUNK),
60 M_DEVBUF, M_INTWAIT | M_ZERO);
62 /* Make sure we have something to copy before we copy it */
63 if (ea->nelem) {
64 bcopy(ea->ps, space, sizeof(void *) * ea->nelem);
65 kfree(ea->ps, M_DEVBUF);
68 ea->ps = space;
69 ea->nelem = index + EXTEND_CHUNK;
71 if (ea->ps[index]) {
72 kprintf("extend_set: entry %d already has storage.\n", index);
73 return 0;
75 else
76 ea->ps[index] = value;
78 return value;
81 void *
82 cam_extend_get(struct extend_array *ea,
83 int index)
85 if (ea == NULL || index >= ea->nelem || index < 0)
86 return NULL;
87 return ea->ps[index];
90 void
91 cam_extend_release(struct extend_array *ea, int index)
93 void *p = cam_extend_get(ea, index);
94 if (p) {
95 ea->ps[index] = 0;