| 1 |
|
| /* mds.c — public entry point for the new Markdown::Simple parser. |
| 2 |
|
| * |
| 3 |
|
| * Pipeline: arena init -> block scanner -> SAX -> HTML renderer. |
| 4 |
|
| * The inline tokeniser HTML-escapes text bytes downstream. |
| 5 |
|
| */ |
| 6 |
|
| |
| 7 |
|
| #include "mds.h" |
| 8 |
|
| #include "mds_buf.h" |
| 9 |
|
| #include "mds_ctx.h" |
| 10 |
|
| #include "mds_arena.h" |
| 11 |
|
| #include "mds_block.h" |
| 12 |
|
| #include "mds_linkref.h" |
| 13 |
|
| #include "mds_footnote.h" |
| 14 |
|
| #include "mds_render_html.h" |
| 15 |
|
| #include "simd/mds_simd.h" |
| 16 |
|
| |
| 17 |
|
| #include <string.h> |
| 18 |
|
| #include <stdint.h> |
| 19 |
|
| #include <stdlib.h> |
| 20 |
|
| |
| 21 |
|
| /* Opaque blob sized comfortably larger than the renderer's private |
| 22 |
|
| * `render_state`. The renderer placement-initialises this via the |
| 23 |
|
| * pointer returned in ud_out. Keeping it stack-local keeps us |
| 24 |
|
| * thread-safe and costs no allocation for the state itself. |
| 25 |
|
| * |
| 26 |
|
| * The state is not entirely allocation free, though: the renderer grows |
| 27 |
|
| * several buffers through it on demand (alt text, autolink coalescing, |
| 28 |
|
| * the highlight accumulator, the footnote tables). Because the blob is a |
| 29 |
|
| * stack local, nothing reclaims those when it goes out of scope, so every |
| 30 |
|
| * exit path below has to call mds_render_html_cleanup. Before that call |
| 31 |
|
| * existed each render leaked between 60 bytes and a few kilobytes |
| 32 |
|
| * depending on which features the document used. */ |
| 33 |
|
| struct mds_render_state_blob { |
| 34 |
|
| unsigned char bytes[16 * 1024]; |
| 35 |
|
| }; |
| 36 |
|
| |
| 37 |
164109 |
| int mds_render_html_to_sv_toc(pTHX_ |
| 38 |
|
| const char* input, |
| 39 |
|
| size_t len, |
| 40 |
|
| unsigned flags, |
| 41 |
|
| SV* output_sv, |
| 42 |
|
| mds_arena* borrowed_arena, |
| 43 |
|
| mds_block_scratch* borrowed_scratch, |
| 44 |
|
| AV* toc) { |
| 45 |
|
| mds_buf buf; |
| 46 |
|
| struct mds_render_state_blob blob; |
| 47 |
|
| void* ud; |
| 48 |
|
| mds_callbacks cb; |
| 49 |
|
| mds_ctx ctx; |
| 50 |
|
| mds_arena local_arena; |
| 51 |
|
| mds_arena* arena; |
| 52 |
|
| |
| 53 |
|
| /* Pass raw `len` so mds_buf_init_for_input owns the output-size |
| 54 |
|
| * multiplier; avoids double-inflation. */ |
| 55 |
164109 |
| mds_buf_init_for_input(aTHX_ &buf, output_sv, input, len); |
| 56 |
|
| |
| 57 |
164109 |
| memset(&blob, 0, sizeof blob); |
| 58 |
164109 |
| ud = &blob; |
| 59 |
|
| |
| 60 |
164109 |
| memset(&cb, 0, sizeof cb); |
| 61 |
164109 |
| mds_render_html_install(&cb, &ud, &buf, flags, toc); |
| 62 |
|
| |
| 63 |
164109 |
| memset(&ctx, 0, sizeof ctx); |
| 64 |
164109 |
| ctx.input = input; |
| 65 |
164109 |
| ctx.len = len; |
| 66 |
164109 |
| ctx.flags = flags; |
| 67 |
164109 |
| ctx.cb = cb; |
| 68 |
164109 |
| ctx.ud = ud; |
| 69 |
164109 |
| ctx.scratch = borrowed_scratch; |
| 70 |
|
| |
| 71 |
|
| /* If a persistent (borrowed) arena was provided, use |
| 72 |
|
| * it by value and reset (not free) at the end so the warm head page |
| 73 |
|
| * survives between parses. Otherwise allocate a per-call arena. */ |
| 74 |
164109 |
▶ | if (borrowed_arena) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (borrowed_arena) { |
141639 |
22470 |
164109 |
|
| 75 |
141639 |
| arena = borrowed_arena; |
| 76 |
|
| } else { |
| 77 |
22470 |
| arena = &local_arena; |
| 78 |
22470 |
| mds_arena_init(arena); |
| 79 |
|
| } |
| 80 |
|
| /* mds_ctx embeds the arena by value, so callees that take &ctx.arena |
| 81 |
|
| * see the same storage; we copy in/out around mds_block_scan. */ |
| 82 |
164109 |
| ctx.arena = *arena; |
| 83 |
|
| |
| 84 |
|
| /* ---- Preprocessing: validate UTF-8, build line index. ---- */ |
| 85 |
|
| { |
| 86 |
328218 |
| const mds_simd_ops* ops = (flags & MDS_FLAG_NO_SIMD) |
| 87 |
1 |
| ? mds_simd_ops_scalar() |
| 88 |
164109 |
▶ | : mds_simd_get(); |
Branch: 1 branch
| Branch | True | False | Total |
| : mds_simd_get(); |
1 |
164108 |
164109 |
|
| 89 |
|
| |
| 90 |
164109 |
▶ | if ((flags & MDS_FLAG_STRICT_UTF8) && len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((flags & MDS_FLAG_STRICT_UTF8) && len) { |
3 |
164106 |
164109 |
| if ((flags & MDS_FLAG_STRICT_UTF8) && len) { |
3 |
0 |
3 |
|
| 91 |
3 |
▶ | if (MDS_UNLIKELY(!ops->validate_utf8(input, len))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (MDS_UNLIKELY(!ops->validate_utf8(input, len))) { |
2 |
1 |
3 |
|
| 92 |
2 |
| mds_arena_snapshot(&ctx.arena, &mds_last_arena_profile); |
| 93 |
2 |
| *arena = ctx.arena; |
| 94 |
2 |
▶ | if (borrowed_arena) mds_arena_reset(arena); |
Branch: 1 branch
| Branch | True | False | Total |
| if (borrowed_arena) mds_arena_reset(arena); |
1 |
1 |
2 |
|
| 95 |
1 |
| else mds_arena_free(arena); |
| 96 |
2 |
| mds_render_html_cleanup(ud); |
| 97 |
2 |
| mds_buf_finalize(aTHX_ &buf); |
| 98 |
2 |
| return -1; /* malformed UTF-8 */ |
| 99 |
|
| } |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
164107 |
▶ | if (len && len <= 0xFFFFFFFFu) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len && len <= 0xFFFFFFFFu) { |
164104 |
3 |
164107 |
| if (len && len <= 0xFFFFFFFFu) { |
164104 |
0 |
164104 |
|
| 103 |
|
| /* Conservative cap: at most one newline per byte. We cap at |
| 104 |
|
| * len so worst-case all-newline inputs still fit. */ |
| 105 |
164104 |
| size_t cap = len; |
| 106 |
164104 |
| uint32_t* offs = (uint32_t*)mds_arena_alloc(&ctx.arena, |
| 107 |
|
| cap * sizeof(uint32_t)); |
| 108 |
164104 |
▶ | if (offs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offs) { |
164104 |
0 |
164104 |
|
| 109 |
164104 |
| size_t n = ops->find_newlines(input, len, offs, cap); |
| 110 |
164104 |
▶ | if (n == (size_t)-1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == (size_t)-1) { |
0 |
164104 |
164104 |
|
| 111 |
0 |
| ctx.line_idx_overflow = 1; |
| 112 |
0 |
| ctx.line_offsets = NULL; |
| 113 |
0 |
| ctx.n_lines = 0; |
| 114 |
|
| } else { |
| 115 |
164104 |
| ctx.line_offsets = offs; |
| 116 |
164104 |
| ctx.n_lines = n; |
| 117 |
|
| } |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
|
| /* ctx.line_offsets is precomputed and validated by tests, but the |
| 121 |
|
| * scanner still does its own per-line walk. */ |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
164107 |
| mds_block_scan(&ctx); |
| 125 |
|
| |
| 126 |
|
| /* Both tables live in the arena but grow their entry arrays off the |
| 127 |
|
| * general heap, so they have to be released while the arena that holds |
| 128 |
|
| * the table structs is still valid. */ |
| 129 |
164107 |
| mds_linkref_free(ctx.refs); |
| 130 |
164107 |
| mds_footnote_free(ctx.footnotes); |
| 131 |
|
| |
| 132 |
164107 |
| mds_arena_snapshot(&ctx.arena, &mds_last_arena_profile); |
| 133 |
164107 |
| *arena = ctx.arena; |
| 134 |
164107 |
▶ | if (borrowed_arena) mds_arena_reset(arena); |
Branch: 1 branch
| Branch | True | False | Total |
| if (borrowed_arena) mds_arena_reset(arena); |
141638 |
22469 |
164107 |
|
| 135 |
22469 |
| else mds_arena_free(arena); |
| 136 |
164107 |
| mds_render_html_cleanup(ud); |
| 137 |
164107 |
| mds_buf_finalize(aTHX_ &buf); |
| 138 |
164107 |
| return 0; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
121425 |
| int mds_render_html_to_sv_ex(pTHX_ |
| 142 |
|
| const char* input, |
| 143 |
|
| size_t len, |
| 144 |
|
| unsigned flags, |
| 145 |
|
| SV* output_sv, |
| 146 |
|
| mds_arena* borrowed_arena, |
| 147 |
|
| mds_block_scratch* borrowed_scratch) { |
| 148 |
121425 |
| return mds_render_html_to_sv_toc(aTHX_ input, len, flags, output_sv, |
| 149 |
|
| borrowed_arena, borrowed_scratch, NULL); |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
22465 |
| int mds_render_html_to_sv(pTHX_ |
| 153 |
|
| const char* input, |
| 154 |
|
| size_t len, |
| 155 |
|
| unsigned flags, |
| 156 |
|
| SV* output_sv) { |
| 157 |
22465 |
| return mds_render_html_to_sv_toc(aTHX_ input, len, flags, output_sv, |
| 158 |
|
| NULL, NULL, NULL); |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
|
| /* Last-parse arena snapshot, populated unconditionally |
| 162 |
|
| * by mds_render_html_to_sv. Not thread-safe; intended for profiling |
| 163 |
|
| * scripts (bench/profile_arena.pl). */ |
| 164 |
|
| mds_arena_profile mds_last_arena_profile; |