1 /* sane - Scanner Access Now Easy.
2 Copyright (C) 1997 Geoffrey T. Dairiki
3 This file is part of the SANE package.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 As a special exception, the authors of SANE give permission for
19 additional uses of the libraries contained in this release of SANE.
20
21 The exception is that, if you link a SANE library with other files
22 to produce an executable, this does not by itself cause the
23 resulting executable to be covered by the GNU General Public
24 License. Your use of that executable is in no way restricted on
25 account of linking the SANE library code into it.
26
27 This exception does not, however, invalidate any other reasons why
28 the executable file might be covered by the GNU General Public
29 License.
30
31 If you submit changes to SANE to the maintainers to be included in
32 a subsequent release, you agree by submitting the changes that
33 those changes may be distributed with this exception intact.
34
35 If you write modifications of your own for SANE, it is your choice
36 whether to permit this exception to apply to your modifications.
37 If you do not wish that, delete this exception notice.
38
39 This file is part of a SANE backend for HP Scanners supporting
40 HP Scanner Control Language (SCL).
41 */
42 /*
43 #define STUBS
44 extern int sanei_debug_hp;*/
45 #define DEBUG_DECLARE_ONLY
46 #include "../include/sane/config.h"
47
48 #include <stdlib.h>
49 #include <string.h>
50 #include "../include/lassert.h"
51
52 #include "hp.h"
53
54 typedef struct hp_alloc_s Alloc;
55
56 struct hp_alloc_s
57 {
58 Alloc * prev;
59 Alloc * next;
60 hp_byte_t buf[1];
61 };
62
63 static Alloc head[] = {{ head, head, {0} }};
64
65 #define DATA_OFFSET (head->buf - (hp_byte_t *)head)
66 #define VOID_TO_ALLOCP(p) ((Alloc *)((hp_byte_t *)(p) - DATA_OFFSET))
67 #define ALLOCSIZE(sz) (sz + DATA_OFFSET)
68
69
70 void *
sanei_hp_alloc(size_t sz)71 sanei_hp_alloc (size_t sz)
72 {
73 Alloc * new = malloc(ALLOCSIZE(sz));
74
75 if (!new)
76 return 0;
77 (new->next = head->next)->prev = new;
78 (new->prev = head)->next = new;
79 return new->buf;
80 }
81
82 void *
sanei_hp_allocz(size_t sz)83 sanei_hp_allocz (size_t sz)
84 {
85 void * new = sanei_hp_alloc(sz);
86
87 if (!new)
88 return 0;
89 memset(new, 0, sz);
90 return new;
91 }
92
93 void *
sanei_hp_memdup(const void * src,size_t sz)94 sanei_hp_memdup (const void * src, size_t sz)
95 {
96 char * new = sanei_hp_alloc(sz);
97 if (!new)
98 return 0;
99 return memcpy(new, src, sz);
100 }
101
102 char *
sanei_hp_strdup(const char * str)103 sanei_hp_strdup (const char * str)
104 {
105 return sanei_hp_memdup(str, strlen(str) + 1);
106 }
107
108 void *
sanei_hp_realloc(void * ptr,size_t sz)109 sanei_hp_realloc (void * ptr, size_t sz)
110 {
111 if (ptr)
112 {
113 Alloc * old = VOID_TO_ALLOCP(ptr);
114 Alloc copy = *old;
115 Alloc * new = realloc(old, ALLOCSIZE(sz));
116 if (!new)
117 return 0;
118 if (new != old)
119 (new->prev = copy.prev)->next = (new->next = copy.next)->prev = new;
120 return new->buf;
121 }
122 else
123 return sanei_hp_alloc(sz);
124 }
125
126 void
sanei_hp_free(void * ptr)127 sanei_hp_free (void * ptr)
128 {
129 Alloc * old = VOID_TO_ALLOCP(ptr);
130
131 assert(old && old != head);
132 (old->next->prev = old->prev)->next = old->next;
133 old->next = old->prev = 0; /* so we can puke on multiple free's */
134 free(old);
135 }
136
137 void
sanei_hp_free_all(void)138 sanei_hp_free_all (void)
139 {
140 Alloc * ptr;
141 Alloc * next;
142
143 for (ptr = head->next; ptr != head; ptr = next)
144 {
145 next = ptr->next;
146 free(ptr);
147 }
148 head->next = head->prev = head;
149 }
150