1
/* zutil.c -- target dependent utility functions for the compression library
2
* Copyright (C) 1995-2002 Jean-loup Gailly.
3
* For conditions of distribution and use, see copyright notice in zlib.h
6
/* @(#) $Id: zutil.c,v 1.2 2002/11/06 22:32:54 davidT Exp $ */
11
extern void exit OF((int));
17
void zmemcpy(dest, source, len)
24
*dest++ = *source++; /* ??? to be unrolled */
28
int zmemcmp(s1, s2, len)
35
for (j = 0; j < len; j++) {
36
if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
41
void zmemzero(dest, len)
47
*dest++ = 0; /* ??? to be unrolled */
53
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
54
/* Small and medium model in Turbo C are for now limited to near allocation
55
* with reduced MAX_WBITS and MAX_MEM_LEVEL
59
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
60
* and farmalloc(64K) returns a pointer with an offset of 8, so we
61
* must fix the pointer. Warning: the pointer must be put back to its
62
* original form in order to free it, use zcfree().
68
local int next_ptr = 0;
70
typedef struct ptr_table_s {
75
local ptr_table table[MAX_PTR];
76
/* This table is used to remember the original form of pointers
77
* to large buffers (64K). Such pointers are normalized with a zero offset.
78
* Since MSDOS is not a preemptive multitasking OS, this table is not
79
* protected from concurrent access. This hack doesn't work anyway on
80
* a protected system like OS/2. Use Microsoft C instead.
83
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
85
voidpf buf = opaque; /* just to make some compilers happy */
86
ulg bsize = (ulg)items*size;
88
/* If we allocate less than 65520 bytes, we assume that farmalloc
89
* will return a usable pointer which doesn't have to be normalized.
92
buf = farmalloc(bsize);
93
if (*(ush*)&buf != 0) return buf;
95
buf = farmalloc(bsize + 16L);
97
if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
98
table[next_ptr].org_ptr = buf;
100
/* Normalize the pointer to seg:0 */
101
*((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
103
table[next_ptr++].new_ptr = buf;
107
void zcfree (voidpf opaque, voidpf ptr)
110
if (*(ush*)&ptr != 0) { /* object < 64K */
114
/* Find the original pointer */
115
for (n = 0; n < next_ptr; n++) {
116
if (ptr != table[n].new_ptr) continue;
118
farfree(table[n].org_ptr);
119
while (++n < next_ptr) {
120
table[n-1] = table[n];
125
ptr = opaque; /* just to make some compilers happy */
126
Assert(0, "zcfree: ptr not found");
129
#endif /* __TURBOC__ */
132
#if defined(M_I86) && !defined(__32BIT__)
133
/* Microsoft C in 16-bit mode */
137
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
138
# define _halloc halloc
139
# define _hfree hfree
142
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
144
if (opaque) opaque = 0; /* to make compiler happy */
145
return _halloc((long)items, size);
148
void zcfree (voidpf opaque, voidpf ptr)
150
if (opaque) opaque = 0; /* to make compiler happy */
157
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
160
extern voidp calloc OF((uInt items, uInt size));
161
extern void free OF((voidpf ptr));
164
voidpf zcalloc (opaque, items, size)
169
if (opaque) items += size - size; /* make compiler happy */
170
return (voidpf)calloc(items, size);
173
void zcfree (opaque, ptr)
178
if (opaque) return; /* make compiler happy */
181
#endif /* MY_ZCALLOC */