on master help sign in
installUnicodeWidthTable: on range population failure, deallocate the custom table
commite07f287d07498bbc8b3d6463958de89a8fa61682 authorJonathan Daugherty <cygnus@foobox.com> dateTue 3 Mar 2020, 16:17 (7 years ago) parent7845acd
2 files changed+20−1
cbits/mk_wcwidth.c+15 src/Graphics/Vty/UnicodeWidthTable/Install.hs+5−1
cbits/mk_wcwidth.c+15
@@ -316,3 +316,18 @@ int vty_custom_table_ready()
316316{
317317 return ((custom_table != NULL) && custom_table_ready);
318318}
319+
320+// Deallocate the custom width table.
321+//
322+// This does nothing if there is no allocated custom width table, or if
323+// there is one but it is in use (marked ready). This is only useful if
324+// an initial allocation succeeds, but range population fails, after
325+// which point the application may want to deallocate the table to avoid
326+// leaving it in an intermediate state.
327+void vty_deallocate_custom_table()
328+{
329+ if ((custom_table != NULL) && (!custom_table_ready)) {
330+ free(custom_table);
331+ custom_table = NULL;
332+ }
333+}
src/Graphics/Vty/UnicodeWidthTable/Install.hs+5−1
@@ -27,6 +27,9 @@ foreign import ccall unsafe "vty_activate_custom_table"
2727foreign import ccall unsafe "vty_custom_table_ready"
2828 c_isCustomTableReady :: IO Int
2929
30+foreign import ccall unsafe "vty_deallocate_custom_table"
31+ deallocateCustomTable :: IO ()
32+
3033-- | Returns True if and only if a custom table has been allocated and
3134-- marked as ready for use. Returns False otherwise.
3235isCustomTableReady :: IO Bool
@@ -80,7 +83,8 @@ installUnicodeWidthTable table = do
8083 (rangeSize r)
8184 (rangeColumns r)
8285
83− when (result /= 0) $
86+ when (result /= 0) $ do
87+ deallocateCustomTable
8488 error $ "installUnicodeWidthTable: error installing range " <>
8589 show r <> ", status " <> show result
8690