Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/librecsort/rshdr.h
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1996-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* Glenn Fowler <[email protected]> *
19
* *
20
***********************************************************************/
21
#ifndef _RSHDR_H
22
#define _RSHDR_H 1
23
24
#if _PACKAGE_ast
25
#include <ast.h>
26
#include <recfmt.h>
27
#endif
28
29
#include <vmalloc.h>
30
31
#include "FEATURE/recsort"
32
33
/* because of sfio's sfreserve() semantics,
34
** the below parameter also limits the max size of a record that can be
35
** processed in rsmerge().
36
*/
37
#define RS_RESERVE (128*1024) /* for I/O reservation */
38
39
#define _RS_PRIVATE_ \
40
Vmdisc_t vmdisc; /* vmalloc discipline */ \
41
unsigned long events; /* active events */ \
42
Void_t* methdata; /* private method data */ \
43
Vmalloc_t* vm; /* region to allocate temp data */ \
44
ssize_t c_max; /* max datasize per chain */ \
45
ssize_t c_size; /* current size */ \
46
Rsobj_t* sorted; /* defined after a reclist call */ \
47
Rsobj_t** list; /* list of processed contexts */ \
48
int n_list; /* number of such contexts */ \
49
Rsobj_t* free; /* free list of objects */ \
50
Rskey_t* key; /* rsinit() key coder state */ \
51
Sfio_t* f; /* current output stream */ \
52
unsigned char *rsrv, *endrsrv, *cur; /* for fast writes */
53
54
#include "recsort.h"
55
56
/* internal control bits */
57
#define RS_SORTED 010000 /* context has been sorted */
58
#define RS_LOCAL 020000 /* local call */
59
60
#if !_PACKAGE_ast
61
#if __STD_C
62
#include <string.h>
63
#endif
64
#endif
65
66
#ifndef uchar
67
#define uchar unsigned char
68
#endif
69
#ifndef ushort
70
#define ushort unsigned short
71
#endif
72
#ifndef uint
73
#define uint unsigned int
74
#endif
75
#ifndef ulong
76
#define ulong unsigned long
77
#endif
78
#ifndef reg
79
#define reg register
80
#endif
81
#ifndef NIL
82
#define NIL(type) ((type)0)
83
#endif
84
85
#ifndef UCHAR_MAX
86
#define UCHAR_MAX ((uchar)(~0) )
87
#endif
88
#ifndef UINT_MAX
89
#define UINT_MAX ((uint)(~0) )
90
#endif
91
#ifndef INT_MAX
92
#define INT_MAX ((int)(UINT_MAX >> 1) )
93
#endif
94
95
/* splay tree operations */
96
#define RLINK(r,x) (r = r->left = x)
97
#define LLINK(l,x) (l = l->right = x)
98
#define RROTATE(r,t) (r->left = t->right, t->right = r, r = t)
99
#define LROTATE(r,t) (r->right = t->left, t->left = r, r = t)
100
101
#define SETLOCAL(rs) (rs->type |= RS_LOCAL)
102
#define GETLOCAL(rs,l) ((l = (rs->type&RS_LOCAL)), (rs->type &= ~RS_LOCAL), l)
103
#define RSWRITE(rs,f,t) (SETLOCAL(rs), rswrite(rs,f,t))
104
105
/* do quick key comparisons using first 4 bytes */
106
#if SIZEOF_LONG == 8
107
#define OBJHEAD(obj) \
108
{ reg uchar* k = obj->key; reg ulong h = 0; \
109
switch(obj->keylen) \
110
{ default : h = ((ulong)k[7]); \
111
case 7 : h |= ((ulong)k[6]) << (1*CHAR_BIT); \
112
case 6 : h |= ((ulong)k[5]) << (2*CHAR_BIT); \
113
case 5 : h |= ((ulong)k[4]) << (3*CHAR_BIT); \
114
case 4 : h |= ((ulong)k[3]) << (4*CHAR_BIT); \
115
case 3 : h |= ((ulong)k[2]) << (5*CHAR_BIT); \
116
case 2 : h |= ((ulong)k[1]) << (6*CHAR_BIT); \
117
case 1 : h |= ((ulong)k[0]) << (7*CHAR_BIT); \
118
case 0 : obj->order = h; \
119
} \
120
}
121
#else /* SIZEOF_LONG == 4*/
122
#define OBJHEAD(obj) \
123
{ reg uchar* k = obj->key; reg ulong h = 0; \
124
switch(obj->keylen) \
125
{ default : h = k[3]; \
126
case 3 : h |= k[2] << (1*CHAR_BIT); \
127
case 2 : h |= k[1] << (2*CHAR_BIT); \
128
case 1 : h |= k[0] << (3*CHAR_BIT); \
129
case 0 : obj->order = h; \
130
} \
131
}
132
#endif
133
134
#define OBJCMP(one,two,cmp) \
135
{ if((one)->order != (two)->order ) \
136
cmp = (one)->order < (two)->order ? -1 : 1; \
137
else \
138
{ reg uchar *ok, *tk; reg ssize_t l, d; \
139
ok = (one)->key+SIZEOF_LONG; tk = (two)->key+SIZEOF_LONG; \
140
if((d = (l = (one)->keylen) - (two)->keylen) > 0) l -= d; \
141
for(l -= SIZEOF_LONG;;) \
142
{ if(l-- <= 0) { cmp = d; break; } \
143
else if((cmp = *ok++ - *tk++) ) break; \
144
} \
145
} \
146
}
147
148
#define MEMCPY(to,fr,n) \
149
switch(n) \
150
{ default: memcpy(to,fr,n); to += n; fr += n; break; \
151
case 8 : *to++ = *fr++; \
152
case 7 : *to++ = *fr++; \
153
case 6 : *to++ = *fr++; \
154
case 5 : *to++ = *fr++; \
155
case 4 : *to++ = *fr++; \
156
case 3 : *to++ = *fr++; \
157
case 2 : *to++ = *fr++; \
158
case 1 : *to++ = *fr++; \
159
}
160
161
/* merging equivalent records */
162
#define EQUAL(r,o,t) \
163
{ if((t = r->equal) ) \
164
{ t->left = (t->left->right = o); } \
165
else { r->equal = (o->left = o); } \
166
}
167
168
#if !_PACKAGE_ast && !__STD_C
169
_BEGIN_EXTERNS_
170
Kpvimport Void_t* memchr _ARG_((const Void_t*, int, size_t));
171
Kpvimport Void_t* memcpy _ARG_((Void_t*, const Void_t*, size_t));
172
_END_EXTERNS_
173
#endif
174
175
#define RSNOTIFY(r,o,v,x,d) ((r->events&o)?rsnotify(r,o,(Void_t*)v,(Void_t*)x,d):(0))
176
177
#define rsnotify _rs_notify
178
179
extern int rsnotify _ARG_((Rs_t*, int, Void_t*, Void_t*, Rsdisc_t*));
180
181
#endif /*_RSHDR_H*/
182
183