refactoring

This commit is contained in:
Maxim Devaev
2022-07-20 06:05:05 +03:00
parent cbee3adb2e
commit 5c747a5b5d
12 changed files with 33 additions and 61 deletions

View File

@@ -67,9 +67,7 @@ void us_output_file_write(void *v_output, const us_frame_s *frame) {
void us_output_file_destroy(void *v_output) {
us_output_file_s *output = (us_output_file_s *)v_output;
if (output->base64_data) {
free(output->base64_data);
}
US_DELETE(output->base64_data, free);
if (output->fp && output->fp != stdout) {
if (fclose(output->fp) < 0) {
US_LOG_PERROR("Can't close output file");

View File

@@ -300,9 +300,7 @@ static int _dump_sink(
retval = -1;
ok:
if (sink) {
us_memsink_destroy(sink);
}
US_DELETE(sink, us_memsink_destroy);
us_frame_destroy(frame);
US_LOG_INFO("Bye-bye");

View File

@@ -32,9 +32,7 @@ us_frame_s *us_frame_init(void) {
}
void us_frame_destroy(us_frame_s *frame) {
if (frame->data) {
free(frame->data);
}
US_DELETE(frame->data, free);
free(frame);
}

View File

@@ -36,7 +36,6 @@ us_memsink_s *us_memsink_init(
sink->client_ttl = client_ttl;
sink->timeout = timeout;
sink->fd = -1;
sink->mem = MAP_FAILED;
atomic_init(&sink->has_clients, false);
US_LOG_INFO("Using %s-sink: %s", name, obj);
@@ -68,7 +67,7 @@ us_memsink_s *us_memsink_init(
}
void us_memsink_destroy(us_memsink_s *sink) {
if (sink->mem != MAP_FAILED) {
if (sink->mem != NULL) {
if (us_memsink_shared_unmap(sink->mem) < 0) {
US_LOG_PERROR("%s-sink: Can't unmap shared memory", sink->name);
}

View File

@@ -185,7 +185,7 @@ void us_device_close(us_device_s *dev) {
}
if (dev->io_method == V4L2_MEMORY_MMAP) {
if (HW(raw.allocated) > 0 && HW(raw.data) != MAP_FAILED) {
if (HW(raw.allocated) > 0 && HW(raw.data) != NULL) {
if (munmap(HW(raw.data), HW(raw.allocated)) < 0) {
US_LOG_PERROR("Can't unmap device buffer=%u", index);
}
@@ -694,6 +694,7 @@ static int _device_open_io_method_mmap(us_device_s *dev) {
US_LOG_PERROR("Can't map device buffer=%u", _RUN(n_bufs));
return -1;
}
assert(HW(raw.data) != NULL);
HW(raw.allocated) = buf.length;
# undef HW

View File

@@ -93,7 +93,7 @@ void us_server_destroy(us_server_s *server) {
}
evhttp_free(_RUN(http));
if (_RUN(ext_fd)) {
if (_RUN(ext_fd) >= 0) {
close(_RUN(ext_fd));
}
event_base_free(_RUN(base));
@@ -108,9 +108,7 @@ void us_server_destroy(us_server_s *server) {
free(client);
});
if (_RUN(auth_token)) {
free(_RUN(auth_token));
}
US_DELETE(_RUN(auth_token), free);
us_frame_destroy(_EX(frame));
free(_RUN(exposed));
@@ -377,18 +375,10 @@ static void _http_callback_static(struct evhttp_request *request, void *v_server
if (fd >= 0) {
close(fd);
}
if (static_path) {
free(static_path);
}
if (buf) {
evbuffer_free(buf);
}
if (decoded_path) {
free(decoded_path);
}
if (uri) {
evhttp_uri_free(uri);
}
US_DELETE(static_path, free);
US_DELETE(buf, evbuffer_free);
US_DELETE(decoded_path, free);
US_DELETE(uri, evhttp_uri_free);
}
#undef COMPAT_REQUEST
@@ -927,9 +917,7 @@ static char *_http_get_client_hostport(struct evhttp_request *request) {
const char *xff = _http_get_header(request, "X-Forwarded-For");
if (xff) {
if (addr) {
free(addr);
}
US_DELETE(addr, free);
assert(addr = strndup(xff, 1024));
for (unsigned index = 0; addr[index]; ++index) {
if (addr[index] == ',') {

View File

@@ -312,10 +312,11 @@ static int _m2m_encoder_init_buffers(
MAP_SHARED,
_RUN(fd),
plane.m.mem_offset
)) == MAP_FAILED) {
)) == NULL) {
_E_LOG_PERROR("Can't map %s buffer=%u", name, *n_bufs_ptr);
goto error;
}
assert((*bufs_ptr)[*n_bufs_ptr].data != NULL);
(*bufs_ptr)[*n_bufs_ptr].allocated = plane.length;
_E_LOG_DEBUG("Queuing %s buffer=%u ...", name, *n_bufs_ptr);
@@ -347,7 +348,7 @@ static void _m2m_encoder_cleanup(us_m2m_encoder_s *enc) {
# define DESTROY_BUFFERS(x_name, x_target) { \
if (_RUN(x_target##_bufs)) { \
for (unsigned m_index = 0; m_index < _RUN(n_##x_target##_bufs); ++m_index) { \
if (_RUN(x_target##_bufs[m_index].allocated) > 0 && _RUN(x_target##_bufs[m_index].data) != MAP_FAILED) { \
if (_RUN(x_target##_bufs[m_index].allocated) > 0 && _RUN(x_target##_bufs[m_index].data) != NULL) { \
if (munmap(_RUN(x_target##_bufs[m_index].data), _RUN(x_target##_bufs[m_index].allocated)) < 0) { \
_E_LOG_PERROR("Can't unmap %s buffer=%u", #x_name, m_index); \
} \