Skip to content

Commit 4a6d2fb

Browse files
committed
setup: bundle and reference additional private zstd headers
This restores the prior behavior in 0.14. I'm not a fan of the behavior. But it does unblock building/linking against the system libzstd. Closes #129. But the solution is not ideal. CC #106.
1 parent 2faf021 commit 4a6d2fb

File tree

6 files changed

+223
-3
lines changed

6 files changed

+223
-3
lines changed

c-ext/compressor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "python-zstandard.h"
1010

11+
/* TODO pool.h is a private header and we shouldn't rely on it. */
1112
#ifndef ZSTD_SINGLE_FILE
1213
#include "pool.h"
1314
#endif

c-ext/decompressor.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88

99
#include "python-zstandard.h"
10+
11+
/* TODO pool.h is a private header and we shouldn't rely on it. */
1012
#ifndef ZSTD_SINGLE_FILE
1113
#include "pool.h"
1214
#endif

docs/news.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Actions Blocking Release
6565
* Support ``ZSTD_threadPool`` APIs for managing a thread pool.
6666
* Utilize ``ZSTD_getDictID_fromCDict()``?
6767
* Utilize ``ZSTD_DCtx_getParameter()``.
68+
* Stop relying on private libzstd headers and symbols (namely ``pool.h``).
6869

6970
Other Actions Not Blocking Release
7071
---------------------------------------
@@ -84,6 +85,10 @@ Bug Fixes
8485
the case in releases prior to 0.15.0 and the include order was reversed
8586
as part of running ``clang-format``. The old/working order has been
8687
restored. (#128)
88+
* Include some private zstd C headers so we can build the C extension against
89+
a system library. The previous behavior of referencing these headers is
90+
restored. That behave is rather questionable and undermines the desire to
91+
use the system zstd.
8792

8893
0.15.0 (released 2020-12-29)
8994
============================

setup_zstd.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
"c-ext/backend_c.c",
2323
]
2424

25+
zstd_includes = [
26+
"zstd",
27+
"zstd/common",
28+
"zstd/dictBuilder",
29+
]
30+
2531

2632
def get_c_extension(
2733
support_legacy=False,
@@ -52,11 +58,22 @@ def get_c_extension(
5258

5359
sources = sorted(set([os.path.join(actual_root, p) for p in ext_sources]))
5460
local_include_dirs = [os.path.join(actual_root, d) for d in ext_includes]
55-
depends = []
5661

57-
if not system_zstd:
62+
if system_zstd:
63+
# TODO remove this once pool.h dependency goes away.
64+
#
65+
# This effectively causes system zstd mode to pull in our
66+
# local headers instead of the system's. Then we link with the
67+
# system library. This is super sketchy and could result in link
68+
# time errors due to symbol mismatch or even run-time errors if
69+
# APIs behave differently.
70+
local_include_dirs.extend(
71+
[os.path.join(actual_root, d) for d in zstd_includes]
72+
)
73+
else:
5874
local_include_dirs.append(os.path.join(actual_root, "zstd"))
59-
depends = sorted(glob.glob(os.path.join(actual_root, "c-ext", "*")))
75+
76+
depends = sorted(glob.glob(os.path.join(actual_root, "c-ext", "*")))
6077

6178
compiler = distutils.ccompiler.new_compiler()
6279

zstd/common/pool.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under both the BSD-style license (found in the
6+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
7+
* in the COPYING file in the root directory of this source tree).
8+
* You may select, at your option, one of the above-listed licenses.
9+
*/
10+
11+
#ifndef POOL_H
12+
#define POOL_H
13+
14+
#if defined (__cplusplus)
15+
extern "C" {
16+
#endif
17+
18+
19+
#include "zstd_deps.h"
20+
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
21+
#include "../zstd.h"
22+
23+
typedef struct POOL_ctx_s POOL_ctx;
24+
25+
/*! POOL_create() :
26+
* Create a thread pool with at most `numThreads` threads.
27+
* `numThreads` must be at least 1.
28+
* The maximum number of queued jobs before blocking is `queueSize`.
29+
* @return : POOL_ctx pointer on success, else NULL.
30+
*/
31+
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
32+
33+
POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
34+
ZSTD_customMem customMem);
35+
36+
/*! POOL_free() :
37+
* Free a thread pool returned by POOL_create().
38+
*/
39+
void POOL_free(POOL_ctx* ctx);
40+
41+
/*! POOL_resize() :
42+
* Expands or shrinks pool's number of threads.
43+
* This is more efficient than releasing + creating a new context,
44+
* since it tries to preserve and re-use existing threads.
45+
* `numThreads` must be at least 1.
46+
* @return : 0 when resize was successful,
47+
* !0 (typically 1) if there is an error.
48+
* note : only numThreads can be resized, queueSize remains unchanged.
49+
*/
50+
int POOL_resize(POOL_ctx* ctx, size_t numThreads);
51+
52+
/*! POOL_sizeof() :
53+
* @return threadpool memory usage
54+
* note : compatible with NULL (returns 0 in this case)
55+
*/
56+
size_t POOL_sizeof(POOL_ctx* ctx);
57+
58+
/*! POOL_function :
59+
* The function type that can be added to a thread pool.
60+
*/
61+
typedef void (*POOL_function)(void*);
62+
63+
/*! POOL_add() :
64+
* Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
65+
* Possibly blocks until there is room in the queue.
66+
* Note : The function may be executed asynchronously,
67+
* therefore, `opaque` must live until function has been completed.
68+
*/
69+
void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
70+
71+
72+
/*! POOL_tryAdd() :
73+
* Add the job `function(opaque)` to thread pool _if_ a worker is available.
74+
* Returns immediately even if not (does not block).
75+
* @return : 1 if successful, 0 if not.
76+
*/
77+
int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
78+
79+
80+
#if defined (__cplusplus)
81+
}
82+
#endif
83+
84+
#endif

zstd/common/zstd_deps.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2016-2020, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under both the BSD-style license (found in the
6+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
7+
* in the COPYING file in the root directory of this source tree).
8+
* You may select, at your option, one of the above-listed licenses.
9+
*/
10+
11+
/* This file provides common libc dependencies that zstd requires.
12+
* The purpose is to allow replacing this file with a custom implementation
13+
* to compile zstd without libc support.
14+
*/
15+
16+
/* Need:
17+
* NULL
18+
* INT_MAX
19+
* UINT_MAX
20+
* ZSTD_memcpy()
21+
* ZSTD_memset()
22+
* ZSTD_memmove()
23+
*/
24+
#ifndef ZSTD_DEPS_COMMON
25+
#define ZSTD_DEPS_COMMON
26+
27+
#include <limits.h>
28+
#include <stddef.h>
29+
#include <string.h>
30+
31+
#if defined(__GNUC__) && __GNUC__ >= 4
32+
# define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l))
33+
# define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l))
34+
# define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l))
35+
#else
36+
# define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l))
37+
# define ZSTD_memmove(d,s,l) memmove((d),(s),(l))
38+
# define ZSTD_memset(p,v,l) memset((p),(v),(l))
39+
#endif
40+
41+
#endif /* ZSTD_DEPS_COMMON */
42+
43+
/* Need:
44+
* ZSTD_malloc()
45+
* ZSTD_free()
46+
* ZSTD_calloc()
47+
*/
48+
#ifdef ZSTD_DEPS_NEED_MALLOC
49+
#ifndef ZSTD_DEPS_MALLOC
50+
#define ZSTD_DEPS_MALLOC
51+
52+
#include <stdlib.h>
53+
54+
#define ZSTD_malloc(s) malloc(s)
55+
#define ZSTD_calloc(n,s) calloc((n), (s))
56+
#define ZSTD_free(p) free((p))
57+
58+
#endif /* ZSTD_DEPS_MALLOC */
59+
#endif /* ZSTD_DEPS_NEED_MALLOC */
60+
61+
/*
62+
* Provides 64-bit math support.
63+
* Need:
64+
* U64 ZSTD_div64(U64 dividend, U32 divisor)
65+
*/
66+
#ifdef ZSTD_DEPS_NEED_MATH64
67+
#ifndef ZSTD_DEPS_MATH64
68+
#define ZSTD_DEPS_MATH64
69+
70+
#define ZSTD_div64(dividend, divisor) ((dividend) / (divisor))
71+
72+
#endif /* ZSTD_DEPS_MATH64 */
73+
#endif /* ZSTD_DEPS_NEED_MATH64 */
74+
75+
/* Need:
76+
* assert()
77+
*/
78+
#ifdef ZSTD_DEPS_NEED_ASSERT
79+
#ifndef ZSTD_DEPS_ASSERT
80+
#define ZSTD_DEPS_ASSERT
81+
82+
#include <assert.h>
83+
84+
#endif /* ZSTD_DEPS_ASSERT */
85+
#endif /* ZSTD_DEPS_NEED_ASSERT */
86+
87+
/* Need:
88+
* ZSTD_DEBUG_PRINT()
89+
*/
90+
#ifdef ZSTD_DEPS_NEED_IO
91+
#ifndef ZSTD_DEPS_IO
92+
#define ZSTD_DEPS_IO
93+
94+
#include <stdio.h>
95+
#define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
96+
97+
#endif /* ZSTD_DEPS_IO */
98+
#endif /* ZSTD_DEPS_NEED_IO */
99+
100+
/* Only requested when <stdint.h> is known to be present.
101+
* Need:
102+
* intptr_t
103+
*/
104+
#ifdef ZSTD_DEPS_NEED_STDINT
105+
#ifndef ZSTD_DEPS_STDINT
106+
#define ZSTD_DEPS_STDINT
107+
108+
#include <stdint.h>
109+
110+
#endif /* ZSTD_DEPS_STDINT */
111+
#endif /* ZSTD_DEPS_NEED_STDINT */

0 commit comments

Comments
 (0)