Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Proof of concept: put big5 map pointers on heap
  • Loading branch information
erlend-aasland committed Apr 25, 2023
commit 5b8324dfb059da887090ea061f02b85911b60935
33 changes: 24 additions & 9 deletions Modules/cjkcodecs/_codecs_hk.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@
* BIG5HKSCS codec
*/

static const encode_map *big5_encmap = NULL;
static const decode_map *big5_decmap = NULL;
typedef struct {
const encode_map *big5_encmap;
const decode_map *big5_decmap;
int initialized;
} big5_state;

CODEC_INIT(big5hkscs)
{
static int initialized = 0;

if (!initialized && IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap))
big5_state *st = (big5_state *)config;
if (st->initialized) {
return 0;
}
if (IMPORT_MAP(tw, big5, &st->big5_encmap, &st->big5_decmap)) {
return -1;
initialized = 1;
}
st->initialized = 1;
return 0;
}

CODEC_DEINIT(big5hkscs)
{
PyMem_Free((void *)config);
return 0;
}

Expand Down Expand Up @@ -58,6 +65,7 @@ ENCODER(big5hkscs)
insize = 1;
REQUIRE_OUTBUF(2);

big5_state *st = (big5_state *)config;
if (c < 0x10000) {
if (TRYMAP_ENC(big5hkscs_bmp, code, c)) {
if (code == MULTIC) {
Expand Down Expand Up @@ -86,7 +94,7 @@ ENCODER(big5hkscs)
}
}
}
else if (TRYMAP_ENC(big5, code, c))
else if (TRYMAP_ENC_PTR(st->big5_encmap, code, c))
;
else
return 1;
Expand Down Expand Up @@ -127,7 +135,8 @@ DECODER(big5hkscs)
REQUIRE_INBUF(2);

if (0xc6 > c || c > 0xc8 || (c < 0xc7 && INBYTE2 < 0xa1)) {
if (TRYMAP_DEC(big5, decoded, c, INBYTE2)) {
big5_state *st = (big5_state *)config;
if (TRYMAP_DEC_PTR(st->big5_decmap, decoded, c, INBYTE2)) {
OUTCHAR(decoded);
NEXT_IN(2);
continue;
Expand Down Expand Up @@ -189,7 +198,13 @@ BEGIN_MAPPINGS_LIST(3)
END_MAPPINGS_LIST

BEGIN_CODECS_LIST(1)
CODEC_STATELESS_WINIT(big5hkscs)
{
big5_state *config = PyMem_Calloc(1, sizeof(big5_state));
if (config == NULL) {
return -1;
}
CODEC_STATELESS_WINIT(big5hkscs, config)
}
END_CODECS_LIST

I_AM_A_MODULE_FOR(hk)
9 changes: 6 additions & 3 deletions Modules/cjkcodecs/cjkcodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ get_module_state(PyObject *mod)
(m)->bottom]) != NOCHAR)
#define TRYMAP_ENC(charset, assi, uni) \
_TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
#define TRYMAP_ENC_PTR(map, assi, uni) \
_TRYMAP_ENC(&(map)[(uni) >> 8], assi, (uni) & 0xff)

#define _TRYMAP_DEC(m, assi, val) \
((m)->map != NULL && \
Expand All @@ -216,6 +218,8 @@ get_module_state(PyObject *mod)
((assi) = (m)->map[(val) - (m)->bottom]) != UNIINV)
#define TRYMAP_DEC(charset, assi, c1, c2) \
_TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
#define TRYMAP_DEC_PTR(map, assi, c1, c2) \
_TRYMAP_DEC(&(map)[c1], assi, c2)

#define BEGIN_MAPPINGS_LIST(NUM) \
static int \
Expand Down Expand Up @@ -273,8 +277,8 @@ add_codecs(cjkcodecs_module_state *st) \
#define CODEC_STATELESS(enc) \
NEXT_CODEC = (MultibyteCodec){#enc, NULL, NULL, NULL, \
_STATELESS_METHODS(enc)};
#define CODEC_STATELESS_WINIT(enc) \
NEXT_CODEC = (MultibyteCodec){#enc, NULL, \
#define CODEC_STATELESS_WINIT(enc, config) \
NEXT_CODEC = (MultibyteCodec){#enc, config, \
enc##_codec_init, enc##_codec_deinit, \
_STATELESS_METHODS(enc)};

Expand Down Expand Up @@ -483,7 +487,6 @@ deinit_codecs(PyObject *mod)
for (int i = 0; i < st->num_codecs; i++) {
const MultibyteCodec *codec = &st->codec_list[i];
if (codec && codec->codecdeinit) {
fprintf(stderr, "- deinit[%d]: %s\n", i, codec->encoding);
if (codec->codecdeinit(codec->config) < 0) {
PyErr_WriteUnraisable(mod);
}
Expand Down
3 changes: 0 additions & 3 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,6 @@ Modules/_decimal/_decimal.c - _py_float_abs -
Modules/_decimal/_decimal.c - _py_long_bit_length -
Modules/_decimal/_decimal.c - _py_float_as_integer_ratio -
Modules/_elementtree.c - expat_capi -
Modules/cjkcodecs/_codecs_hk.c - big5_encmap -
Modules/cjkcodecs/_codecs_hk.c - big5_decmap -
Modules/cjkcodecs/_codecs_hk.c big5hkscs_codec_init initialized -
Modules/cjkcodecs/_codecs_iso2022.c - cp949_encmap -
Modules/cjkcodecs/_codecs_iso2022.c - ksx1001_decmap -
Modules/cjkcodecs/_codecs_iso2022.c - jisxcommon_encmap -
Expand Down