[dvblast-devel] Dynamic memory allocation configuration ...

debugasm debugasm at gmail.com
Sat Dec 1 12:34:15 CET 2018


I made some changes to the management of dynamic memory in order to 
select an
alternative manager of the one present in the standard libraries 
(malloc, free).

In the file "common.h" select the dynamic memory allocation function:

/*
  * Aliases for C library malloc() in case C library
  * malloc() needs extra protection or other type of
  * dynamic allocation is usated, allow these defines
  * to be overridden.
  */

#ifndef bitstream_malloc
#define bitstream_malloc malloc
#endif
#ifndef bitstream_free
#define bitstream_free free
#endif

During the work of restile I have found some small mistake in some 
functions:

- desc_6a.h do not return value (void)

static inline void desc6a_set_length(uint8_t *p_desc)
{
     uint8_t i_size = (DESC6A_HEADER_SIZE - DESC_HEADER_SIZE)
                        + desc6a_get_component_type_flag(p_desc)
                        + desc6a_get_bsid_flag(p_desc)
                        + desc6a_get_mainid_flag(p_desc)
                        + desc6a_get_asvc_flag(p_desc);
     return desc_set_length(p_desc, i_size);
}

- desc_7a.h do not return value (void)

static inline void desc7a_set_length(uint8_t *p_desc)
{
     uint8_t i_size = (DESC7A_HEADER_SIZE - DESC_HEADER_SIZE)
                        + desc7a_get_component_type_flag(p_desc)
                        + desc7a_get_bsid_flag(p_desc)
                        + desc7a_get_mainid_flag(p_desc)
                        + desc7a_get_asvc_flag(p_desc)
                        + desc7a_get_substream1_flag(p_desc)
                        + desc7a_get_substream2_flag(p_desc)
                        + desc7a_get_substream3_flag(p_desc);
     return desc_set_length(p_desc, i_size);
}

- I have rewrite part of "desc_83p28.h" and "desc_88p28.h"

Function for "set" were absent and "desc_88p28.h" he had the name of the 
wrong
functions.

- In the file "psi.h" I have add a "psi_get_crc" function

Hoping to have done something util, I have attached the file for the patch.

debugasm

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/dvblast-devel/attachments/20181201/dbdb42c6/attachment.html>
-------------- next part --------------
diff -r -u -N -w bitstream-master/common.h bitstream/common.h
--- bitstream-master/common.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/common.h	2018-12-01 12:00:55.638052400 +0100
@@ -31,10 +31,25 @@
 #include <stdint.h>   /* uint8_t, uint16_t, etc... */
 #include <stdbool.h>  /* bool */
 #include <string.h>   /* memset, memcpy */
+#include <stdlib.h>   /* malloc, free */
 
 #define BITSTREAM_VERSION_MAJOR 1
-#define BITSTREAM_VERSION_MINOR 0
-#define BITSTREAM_VERSION_REVISION 0
+#define BITSTREAM_VERSION_MINOR 5
+#define BITSTREAM_VERSION_REVISION 1
+
+/* 
+ * Aliases for C library malloc() in case C library 
+ * malloc() needs extra protection or other type of
+ * dynamic allocation is usated, allow these defines 
+ * to be overridden.
+ */
+
+#ifndef bitstream_malloc
+#define bitstream_malloc malloc
+#endif
+#ifndef bitstream_free
+#define bitstream_free free
+#endif
 
 #ifdef __cplusplus
 extern "C"
diff -r -u -N -w bitstream-master/dvb/si/desc_40.h bitstream/dvb/si/desc_40.h
--- bitstream-master/dvb/si/desc_40.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_40.h	2018-12-01 11:58:29.162186100 +0100
@@ -106,7 +106,7 @@
             pf_print(print_opaque, "    - desc 47 bouquetname=\"%s\"",
                      psz_network_name);
     }
-    free(psz_network_name);
+    bitstream_free(psz_network_name);
 }
 
 #ifdef __cplusplus
diff -r -u -N -w bitstream-master/dvb/si/desc_48.h bitstream/dvb/si/desc_48.h
--- bitstream-master/dvb/si/desc_48.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_48.h	2018-12-01 11:58:37.257509900 +0100
@@ -149,8 +149,8 @@
                  "    - desc 48 service type=0x%hhx provider=\"%s\" service=\"%s\"",
                  desc48_get_type(p_desc), psz_provider, psz_service);
     }
-    free(psz_provider);
-    free(psz_service);
+    bitstream_free(psz_provider);
+    bitstream_free(psz_service);
 }
 
 #ifdef __cplusplus
diff -r -u -N -w bitstream-master/dvb/si/desc_4d.h bitstream/dvb/si/desc_4d.h
--- bitstream-master/dvb/si/desc_4d.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_4d.h	2018-12-01 11:58:45.882854900 +0100
@@ -151,8 +151,8 @@
                  "    - desc 4d short_event lang=%3.3s event_name=\"%s\" text=\"%s\"",
                  (char *)desc4d_get_lang(p_desc), psz_event_name, psz_text);
     }
-    free(psz_event_name);
-    free(psz_text);
+    bitstream_free(psz_event_name);
+    bitstream_free(psz_text);
 }
 
 #ifdef __cplusplus
diff -r -u -N -w bitstream-master/dvb/si/desc_4e.h bitstream/dvb/si/desc_4e.h
--- bitstream-master/dvb/si/desc_4e.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_4e.h	2018-12-01 11:58:53.883174900 +0100
@@ -235,7 +235,7 @@
                  psz_text
                 );
     }
-    free( psz_text );
+    bitstream_free( psz_text );
 
     uint8_t j = 0;
     const uint8_t *p_desc_n;
@@ -258,8 +258,8 @@
                      psz_desc, psz_item);
         }
 
-        free(psz_desc);
-        free(psz_item);
+        bitstream_free(psz_desc);
+        bitstream_free(psz_item);
     }
 
     if (i_print_type == PRINT_XML)
diff -r -u -N -w bitstream-master/dvb/si/desc_50.h bitstream/dvb/si/desc_50.h
--- bitstream-master/dvb/si/desc_50.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_50.h	2018-12-01 11:58:59.738409100 +0100
@@ -147,7 +147,7 @@
                 );
     }
 
-    free(psz_text);
+    bitstream_free(psz_text);
 }
 
 #ifdef __cplusplus
diff -r -u -N -w bitstream-master/dvb/si/desc_5b.h bitstream/dvb/si/desc_5b.h
--- bitstream-master/dvb/si/desc_5b.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_5b.h	2018-12-01 11:59:06.533680900 +0100
@@ -137,7 +137,7 @@
                      psz_network_name
                     );
         }
-        free(psz_network_name);
+        bitstream_free(psz_network_name);
     }
 }
 
diff -r -u -N -w bitstream-master/dvb/si/desc_5d.h bitstream/dvb/si/desc_5d.h
--- bitstream-master/dvb/si/desc_5d.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_5d.h	2018-12-01 11:59:34.409803400 +0100
@@ -158,8 +158,8 @@
                      psz_service_name
                     );
         }
-        free(psz_provider_name);
-        free(psz_service_name);
+        bitstream_free(psz_provider_name);
+        bitstream_free(psz_service_name);
     }
 }
 
diff -r -u -N -w bitstream-master/dvb/si/desc_5e.h bitstream/dvb/si/desc_5e.h
--- bitstream-master/dvb/si/desc_5e.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_5e.h	2018-12-01 11:59:28.304559200 +0100
@@ -145,7 +145,7 @@
                      psz_text
                     );
         }
-        free(psz_text);
+        bitstream_free(psz_text);
     }
 }
 
diff -r -u -N -w bitstream-master/dvb/si/desc_64.h bitstream/dvb/si/desc_64.h
--- bitstream-master/dvb/si/desc_64.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_64.h	2018-12-01 11:59:20.324232500 +0100
@@ -186,7 +186,7 @@
                  (char *)desc64_get_lang(p_desc),
                  psz_text);
     }
-    free(psz_text);
+    bitstream_free(psz_text);
 }
 
 #ifdef __cplusplus
diff -r -u -N -w bitstream-master/dvb/si/desc_6a.h bitstream/dvb/si/desc_6a.h
--- bitstream-master/dvb/si/desc_6a.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_6a.h	2018-12-01 10:55:52.905095600 +0100
@@ -110,7 +110,7 @@
                        + desc6a_get_bsid_flag(p_desc)
                        + desc6a_get_mainid_flag(p_desc)
                        + desc6a_get_asvc_flag(p_desc);
-    return desc_set_length(p_desc, i_size);
+    desc_set_length(p_desc, i_size);
 }
 
 static inline bool desc6a_validate(const uint8_t *p_desc)
diff -r -u -N -w bitstream-master/dvb/si/desc_7a.h bitstream/dvb/si/desc_7a.h
--- bitstream-master/dvb/si/desc_7a.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_7a.h	2018-12-01 10:56:35.080635100 +0100
@@ -140,7 +140,7 @@
                        + desc7a_get_substream1_flag(p_desc)
                        + desc7a_get_substream2_flag(p_desc)
                        + desc7a_get_substream3_flag(p_desc);
-    return desc_set_length(p_desc, i_size);
+    desc_set_length(p_desc, i_size);
 }
 
 static inline bool desc7a_validate(const uint8_t *p_desc)
diff -r -u -N -w bitstream-master/dvb/si/desc_83p28.h bitstream/dvb/si/desc_83p28.h
--- bitstream-master/dvb/si/desc_83p28.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_83p28.h	2018-12-01 11:02:09.843934000 +0100
@@ -64,16 +64,33 @@
     return p_desc_n;
 }
 
+static inline void desc83p28n_set_sid(uint8_t *p_desc_n, uint16_t i_sid)
+{
+    p_desc_n[0] = i_sid >> 8;
+    p_desc_n[1] = i_sid & 0xff;
+}
+
 static inline uint16_t desc83p28n_get_sid(const uint8_t *p_desc_n)
 {
     return (p_desc_n[0] << 8) | p_desc_n[1];
 }
 
+static inline void desc83p28n_set_visible(uint8_t *p_desc_n, bool i_visible)
+{
+    p_desc_n[2] = i_visible ? (p_desc_n[2] | 0x80) : (p_desc_n[2] &~ 0x80); // 1xxxxxxx
+}
+
 static inline bool desc83p28n_get_visible(const uint8_t *p_desc_n)
 {
     return !!(p_desc_n[2] & 0x80);
 }
 
+static inline void desc83p28n_set_lcn(uint8_t *p_desc_n, uint16_t i_lcn)
+{
+    p_desc_n[2] = (i_lcn >> 8) & 0x3;
+    p_desc_n[3] = i_lcn & 0xff;
+}
+
 static inline uint16_t desc83p28n_get_lcn(const uint8_t *p_desc_n)
 {
     return ((p_desc_n[2] & 0x3) << 8) | p_desc_n[3];
diff -r -u -N -w bitstream-master/dvb/si/desc_88p28.h bitstream/dvb/si/desc_88p28.h
--- bitstream-master/dvb/si/desc_88p28.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/desc_88p28.h	2018-12-01 11:06:01.841214300 +0100
@@ -56,9 +56,12 @@
 }
 
 #define desc88p28_get_service desc83p28_get_service
-#define desc88p28_get_sid desc83p28_get_sid
-#define desc88p28_get_visible desc83p28_get_visible
-#define desc88p28_get_lcn desc83p28_get_lcn
+#define desc88p28n_set_sid desc83p28n_set_sid
+#define desc88p28n_get_sid desc83p28n_get_sid
+#define desc88p28n_set_visible desc83p28n_set_visible
+#define desc88p28n_get_visible desc83p28n_get_visible
+#define desc88p28n_set_lcn desc83p28n_set_lcn
+#define desc88p28n_get_lcn desc83p28n_get_lcn
 #define desc88p28_validate desc83p28_validate
 #define desc88p28_print desc83p28_print
 
diff -r -u -N -w bitstream-master/dvb/si/strings.h bitstream/dvb/si/strings.h
--- bitstream-master/dvb/si/strings.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/dvb/si/strings.h	2018-12-01 12:00:26.041868600 +0100
@@ -33,8 +33,6 @@
 #ifndef __BITSTREAM_DVB_STRINGS_H__
 #define __BITSTREAM_DVB_STRINGS_H__
 
-#include <stdlib.h>   /* malloc, free */
-
 #include <bitstream/common.h>
 
 #ifdef __cplusplus
@@ -106,7 +104,7 @@
     int i;
 
     if (!strcmp(psz_encoding, psz_default_encoding)) {
-        uint8_t *p_encoded = (uint8_t *)malloc(i_length);
+        uint8_t *p_encoded = (uint8_t *)bitstream_malloc(i_length);
         *pi_out_length = i_length;
         memcpy(p_encoded, p_string, i_length);
         return p_encoded;
@@ -114,7 +112,7 @@
 
     for (i = 0; ppsz_dvb_encodings[i] != NULL; i++) {
         if (!strcasecmp(psz_encoding, ppsz_dvb_encodings[i])) {
-            uint8_t *p_encoded = (uint8_t *)malloc(i_length + 1);
+            uint8_t *p_encoded = (uint8_t *)bitstream_malloc(i_length + 1);
             *pi_out_length = i_length + 1;
             p_encoded[0] = i;
             memcpy(p_encoded + 1, p_string, i_length);
@@ -124,7 +122,7 @@
 
     for (i = 0; ppsz_dvb_encodings10[i] != NULL; i++) {
         if (!strcasecmp(psz_encoding, ppsz_dvb_encodings10[i])) {
-            uint8_t *p_encoded = (uint8_t *)malloc(i_length + 3);
+            uint8_t *p_encoded = (uint8_t *)bitstream_malloc(i_length + 3);
             *pi_out_length = i_length + 3;
             p_encoded[0] = 0x10;
             p_encoded[1] = 0x0;
@@ -167,7 +165,7 @@
             dvb_string_get_encoding(&p_string, &i_length, psz_default_encoding);
         if (psz_encoding == NULL || !i_length) {
             /* try one-byte charset */
-            char *psz_string = (char *)malloc(i_length + 1);
+            char *psz_string = (char *)bitstream_malloc(i_length + 1);
             memcpy(psz_string, p_string, i_length);
             psz_string[i_length] = '\0';
             return psz_string;
@@ -176,7 +174,7 @@
         return pf_iconv(iconv_opaque, psz_encoding, (char *)p_string, i_length);
     }
 
-    return strdup("");
+    return NULL;
 }
 
 static inline char *dvb_string_get(const uint8_t *p_string, size_t i_length,
@@ -222,7 +220,7 @@
         psz1++;
     }
 
-    psz2 = psz_output = (char *)malloc(i_output_size + 1);
+    psz2 = psz_output = (char *)bitstream_malloc(i_output_size + 1);
     psz1 = psz_input;
     while (*psz1) {
         switch (*psz1) {
@@ -253,7 +251,7 @@
     }
     *psz2 = '\0';
 
-    free(psz_input);
+    bitstream_free(psz_input);
     return psz_output;
 }
 
diff -r -u -N -w bitstream-master/mpeg/psi/psi.h bitstream/mpeg/psi/psi.h
--- bitstream-master/mpeg/psi/psi.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/mpeg/psi/psi.h	2018-12-01 12:01:42.514927400 +0100
@@ -151,12 +151,12 @@
 
 static inline uint8_t *psi_allocate(void)
 {
-    return (uint8_t *)malloc((PSI_MAX_SIZE + PSI_HEADER_SIZE) * sizeof(uint8_t));
+    return (uint8_t *)bitstream_malloc((PSI_MAX_SIZE + PSI_HEADER_SIZE) * sizeof(uint8_t));
 }
 
 static inline uint8_t *psi_private_allocate(void)
 {
-    return (uint8_t *)malloc((PSI_PRIVATE_MAX_SIZE + PSI_HEADER_SIZE) * sizeof(uint8_t));
+    return (uint8_t *)bitstream_malloc((PSI_PRIVATE_MAX_SIZE + PSI_HEADER_SIZE) * sizeof(uint8_t));
 }
 
 static inline void psi_set_tableid(uint8_t *p_section, uint8_t i_table_id)
@@ -273,6 +273,17 @@
     p_section[i_end + 3] = i_crc & 0xff;
 }
 
+static inline uint32_t psi_get_crc(const uint8_t *p_section)
+{
+    uint16_t i_end = (((p_section[1] & 0xf) << 8) | p_section[2])
+                      + PSI_HEADER_SIZE - PSI_CRC_SIZE;
+
+    return ((uint32_t)p_section[i_end] << 24) |
+    	   ((uint32_t)p_section[i_end + 1] << 16) |
+    	   ((uint32_t)p_section[i_end + 2] << 8) |
+    	   ((uint32_t)p_section[i_end + 3]);
+}
+
 static inline bool psi_check_crc(const uint8_t *p_section)
 {
     uint32_t i_crc = 0xffffffff;
@@ -323,7 +334,7 @@
 static inline void psi_assemble_reset(uint8_t **pp_psi_buffer,
                                       uint16_t *pi_psi_buffer_used)
 {
-    free(*pp_psi_buffer);
+    bitstream_free(*pp_psi_buffer);
     psi_assemble_init(pp_psi_buffer, pi_psi_buffer_used);
 }
 
@@ -440,7 +451,7 @@
 
 static inline uint8_t **psi_table_allocate(void)
 {
-    return (uint8_t **)malloc(PSI_TABLE_MAX_SECTIONS * sizeof(uint8_t *));
+    return (uint8_t **)bitstream_malloc(PSI_TABLE_MAX_SECTIONS * sizeof(uint8_t *));
 }
 
 static inline void psi_table_init(uint8_t **pp_sections)
@@ -454,7 +465,7 @@
 {
     int i;
     for (i = 0; i < PSI_TABLE_MAX_SECTIONS; i++)
-        free(pp_sections[i]);
+        bitstream_free(pp_sections[i]);
 }
 
 static inline bool psi_table_validate(uint8_t * const *pp_sections)
@@ -486,7 +497,7 @@
     uint16_t i_tableidext = psi_get_tableidext( p_section );
     int i;
 
-    free(pp_sections[i_section]);
+    bitstream_free(pp_sections[i_section]);
     pp_sections[i_section] = p_section;
 
     for (i = 0; i <= i_last_section; i++) {
@@ -501,7 +512,7 @@
 
     /* free spurious, invalid sections */
     for (; i < PSI_TABLE_MAX_SECTIONS; i++) {
-        free(pp_sections[i]);
+        bitstream_free(pp_sections[i]);
         pp_sections[i] = NULL;
     }
 
diff -r -u -N -w bitstream-master/mpeg/ts.h bitstream/mpeg/ts.h
--- bitstream-master/mpeg/ts.h	2018-11-23 17:35:14.000000000 +0100
+++ bitstream/mpeg/ts.h	2018-12-01 12:00:37.992346600 +0100
@@ -33,10 +33,7 @@
 #ifndef __BITSTREAM_MPEG_TS_H__
 #define __BITSTREAM_MPEG_TS_H__
 
-#include <stdlib.h>   /* malloc */
-#include <stdint.h>   /* uint8_t, uint16_t, etc... */
-#include <stdbool.h>  /* bool */
-#include <string.h>   /* memset */
+#include <bitstream/common.h>
 
 #ifdef __cplusplus
 extern "C"
@@ -56,7 +53,7 @@
 
 static inline uint8_t *ts_allocate(void)
 {
-    return (uint8_t *)malloc(TS_SIZE * sizeof(uint8_t));
+    return (uint8_t *)bitstream_malloc(TS_SIZE * sizeof(uint8_t));
 }
 
 static inline void ts_init(uint8_t *p_ts)


More information about the dvblast-devel mailing list