YAZ 5.35.1
odr_mem.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
9#if HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include <limits.h>
14#include <stdlib.h>
15#include "odr-priv.h"
16#include <yaz/xmalloc.h>
17
18/* ------------------------ NIBBLE MEMORY ---------------------- */
19
20/*
21 * Extract the memory control block from o.
22 */
24{
25 NMEM r = o->mem;
26
27 o->mem = nmem_create();
28 return r;
29}
30
31void *odr_malloc(ODR o, size_t size)
32{
33 return nmem_malloc(o->mem, size);
34}
35
36char *odr_strdup(ODR o, const char *str)
37{
38 return nmem_strdup(o->mem, str);
39}
40
41char *odr_strdup_null(ODR o, const char *str)
42{
43 return nmem_strdup_null(o->mem, str);
44}
45
46char *odr_strdupn(ODR o, const char *str, size_t n)
47{
48 return nmem_strdupn(o->mem, str, n);
49}
50
52{
53 return nmem_intdup(o->mem, v);
54}
55
57{
58 return nmem_booldup(o->mem, v);
59}
60
61size_t odr_total(ODR o)
62{
63 return nmem_total(o->mem);
64}
65
66Odr_oct *odr_create_Odr_oct(ODR o, const char *buf, int sz)
67{
68 Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
69 p->buf = odr_strdupn(o, buf, sz);
70 p->len = sz;
71 return p;
72}
73
74/* ---------- memory management for data encoding ----------*/
75
76
77int odr_grow_block(ODR b, int min_bytes)
78{
79 int togrow;
80
81 if (!b->op->can_grow)
82 return -1;
83 if (!b->op->size)
84 togrow = 1024;
85 else
86 togrow = b->op->size;
87 if (togrow < min_bytes)
88 togrow = min_bytes;
89 if (b->op->size && !(b->op->buf =
90 (char *) xrealloc(b->op->buf, b->op->size += togrow)))
91 abort();
92 else if (!b->op->size && !(b->op->buf = (char *)
93 xmalloc(b->op->size = togrow)))
94 abort();
95 return 0;
96}
97
98int odr_write(ODR o, const char *buf, int bytes)
99{
100 if (bytes < 0 || o->op->pos > INT_MAX - bytes)
101 {
102 odr_seterror(o, OSPACE, 40);
103 return -1;
104 }
105 if (o->op->pos + bytes >= o->op->size && odr_grow_block(o, bytes))
106 {
107 odr_seterror(o, OSPACE, 40);
108 return -1;
109 }
110 memcpy(o->op->buf + o->op->pos, buf, bytes);
111 o->op->pos += bytes;
112 if (o->op->pos > o->op->top)
113 o->op->top = o->op->pos;
114 return 0;
115}
116
117int odr_seek(ODR o, int whence, int offset)
118{
119 if (whence == ODR_S_CUR)
120 offset += o->op->pos;
121 else if (whence == ODR_S_END)
122 offset += o->op->top;
123 if (offset > o->op->size && odr_grow_block(o, offset - o->op->size))
124 {
125 odr_seterror(o, OSPACE, 41);
126 return -1;
127 }
128 o->op->pos = offset;
129 return 0;
130}
131
132Odr_int odr_strtol(const char *nptr, char **endptr, int base)
133{
134#if NMEM_64
135#if WIN32
136 return _strtoui64(nptr, endptr, base);
137#else
138 return strtoll(nptr, endptr, base);
139#endif
140
141#else
142 return strtol(nptr, endptr, base);
143#endif
144}
145
146Odr_int odr_atoi(const char *s)
147{
148 char *endptr;
149 return odr_strtol(s, &endptr, 10);
150}
151
152/*
153 * Local variables:
154 * c-basic-offset: 4
155 * c-file-style: "Stroustrup"
156 * indent-tabs-mode: nil
157 * End:
158 * vim: shiftwidth=4 tabstop=8 expandtab
159 */
160
NMEM nmem_create(void)
returns new NMEM handle
Definition nmem.c:181
size_t nmem_total(NMEM n)
returns size in bytes of memory for NMEM handle
Definition nmem.c:169
void * nmem_malloc(NMEM n, size_t size)
allocates memory block on NMEM handle
Definition nmem.c:145
nmem_int_t * nmem_intdup(NMEM mem, nmem_int_t v)
allocates and sets integer for NMEM
Definition nmemsdup.c:41
nmem_bool_t * nmem_booldup(NMEM mem, nmem_bool_t v)
allocates and sets boolean for NMEM
Definition nmemsdup.c:48
char * nmem_strdup(NMEM mem, const char *src)
allocates string on NMEM handle (similar strdup)
Definition nmemsdup.c:18
char * nmem_strdupn(NMEM mem, const char *src, size_t n)
allocates string of certain size on NMEM handle
Definition nmemsdup.c:33
char * nmem_strdup_null(NMEM mem, const char *src)
allocates string on NMEM handle - allows NULL ptr buffer
Definition nmemsdup.c:25
Internal ODR definitions.
void odr_seterror(ODR o, int error, int id)
Definition odr.c:118
#define ODR_S_CUR
Definition odr.h:118
nmem_bool_t Odr_bool
Definition odr.h:48
nmem_int_t Odr_int
Definition odr.h:47
#define ODR_S_END
Definition odr.h:119
#define OSPACE
Definition odr.h:153
Odr_oct * odr_create_Odr_oct(ODR o, const char *buf, int sz)
Definition odr_mem.c:66
Odr_int * odr_intdup(ODR o, Odr_int v)
Definition odr_mem.c:51
NMEM odr_extract_mem(ODR o)
Definition odr_mem.c:23
void * odr_malloc(ODR o, size_t size)
Definition odr_mem.c:31
char * odr_strdupn(ODR o, const char *str, size_t n)
Definition odr_mem.c:46
int odr_grow_block(ODR b, int min_bytes)
Definition odr_mem.c:77
size_t odr_total(ODR o)
Definition odr_mem.c:61
Odr_bool * odr_booldup(ODR o, Odr_bool v)
Definition odr_mem.c:56
char * odr_strdup(ODR o, const char *str)
Definition odr_mem.c:36
Odr_int odr_atoi(const char *s)
Definition odr_mem.c:146
int odr_seek(ODR o, int whence, int offset)
Definition odr_mem.c:117
char * odr_strdup_null(ODR o, const char *str)
Definition odr_mem.c:41
int odr_write(ODR o, const char *buf, int bytes)
Definition odr_mem.c:98
Odr_int odr_strtol(const char *nptr, char **endptr, int base)
Definition odr_mem.c:132
char * buf
Definition odr-priv.h:84
Definition odr.h:100
int len
Definition odr.h:102
char * buf
Definition odr.h:101
Definition odr.h:125
struct Odr_private * op
Definition odr.h:132
NMEM mem
Definition odr.h:130
Header for memory handling functions.
#define xrealloc(o, x)
utility macro which calls xrealloc_f
Definition xmalloc.h:47
#define xmalloc(x)
utility macro which calls malloc_f
Definition xmalloc.h:49