on master help sign in
installUnicodeWidthTable: on range population failure, deallocate the custom table
e07f287Jonathan Daugherty
cbits/mk_wcwidth.c+15
@@ -316,3 +316,18 @@ int vty_custom_table_ready()
316{
316{
317 return ((custom_table != NULL) && custom_table_ready);
317 return ((custom_table != NULL) && custom_table_ready);
318}
318}
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"
27foreign import ccall unsafe "vty_custom_table_ready"
27foreign import ccall unsafe "vty_custom_table_ready"
28 c_isCustomTableReady :: IO Int
28 c_isCustomTableReady :: IO Int
29
29
30+foreign import ccall unsafe "vty_deallocate_custom_table"
31+ deallocateCustomTable :: IO ()
32+
30-- | Returns True if and only if a custom table has been allocated and
33-- | Returns True if and only if a custom table has been allocated and
31-- marked as ready for use. Returns False otherwise.
34-- marked as ready for use. Returns False otherwise.
32isCustomTableReady :: IO Bool
35isCustomTableReady :: IO Bool
@@ -80,7 +83,8 @@ installUnicodeWidthTable table = do
80 (rangeSize r)
83 (rangeSize r)
81 (rangeColumns r)
84 (rangeColumns r)
82
85
83− when (result /= 0) $
86+ when (result /= 0) $ do
87+ deallocateCustomTable
84 error $ "installUnicodeWidthTable: error installing range " <>
88 error $ "installUnicodeWidthTable: error installing range " <>
85 show r <> ", status " <> show result
89 show r <> ", status " <> show result
86
90