3
Fork 0

Small optimization and fixing of memory leak

This commit is contained in:
Artem Prilutskiy 2017-12-23 13:31:40 +03:00
parent b140576a6f
commit de6f42c3d2
4 changed files with 44 additions and 28 deletions

View file

@ -19,8 +19,11 @@
#define VALUE_CORD_INCOMING_SOURCE_ID 4
#define VALUE_CORD_TALKER_ALIAS 7
#define BANNER_RENEWAL_INTERVAL 60
PatchCord::PatchCord(uint32_t network, uint32_t link)
{
renewal = 0;
banner = NULL;
number = link;
asprintf(&name, SERVICE_NAME ".N%d", network);
@ -59,6 +62,15 @@ void PatchCord::setTalkerAlias(const char* value)
void PatchCord::getContextBanner()
{
time_t now = time(NULL);
if ((banner != NULL) &&
(renewal >= now))
{
// Save RPC calls and use cached value
return;
}
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextList");
@ -83,13 +95,19 @@ void PatchCord::getContextBanner()
DBUS_TYPE_INVALID)) &&
(count > 0))
{
free(banner);
banner = strdup(*array);
if ((banner == NULL) ||
(strcmp(banner, *array) != 0))
{
free(banner);
banner = strdup(*array);
}
dbus_free_string_array(array);
}
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
renewal = now + BANNER_RENEWAL_INTERVAL;
}
void PatchCord::invokeCommand(const char* command)
@ -112,9 +130,6 @@ void PatchCord::invokeCommand(const char* command)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
}
void PatchCord::setSpecificValue(uint32_t key, uint32_t value)
@ -138,9 +153,6 @@ void PatchCord::setSpecificValue(uint32_t key, uint32_t value)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
}
uint32_t PatchCord::getSpecificValue(uint32_t key)
@ -184,12 +196,8 @@ uint32_t PatchCord::getSpecificValue(uint32_t key)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
return value;
}
@ -240,7 +248,6 @@ uint32_t PatchCord::getPrivateIDForCall(const char* call)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
return value;
@ -297,7 +304,6 @@ bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
return result;

View file

@ -3,6 +3,7 @@
#ifndef PATCHCORDPROXY_H
#define PATCHCORDPROXY_H
#include <time.h>
#include <stdint.h>
#include <dbus/dbus.h>
@ -30,6 +31,7 @@ class PatchCord
char* name;
uint32_t number;
time_t renewal;
char* banner;
void getContextBanner();

View file

@ -19,8 +19,11 @@
#define VALUE_CORD_INCOMING_SOURCE_ID 4
#define VALUE_CORD_TALKER_ALIAS 7
#define BANNER_RENEWAL_INTERVAL 60
PatchCord::PatchCord(uint32_t network, uint32_t link)
{
renewal = 0;
banner = NULL;
number = link;
asprintf(&name, SERVICE_NAME ".N%d", network);
@ -59,6 +62,15 @@ void PatchCord::setTalkerAlias(const char* value)
void PatchCord::getContextBanner()
{
time_t now = time(NULL);
if ((banner != NULL) &&
(renewal >= now))
{
// Save RPC calls and use cached value
return;
}
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextList");
@ -83,13 +95,19 @@ void PatchCord::getContextBanner()
DBUS_TYPE_INVALID)) &&
(count > 0))
{
free(banner);
banner = strdup(*array);
if ((banner == NULL) ||
(strcmp(banner, *array) != 0))
{
free(banner);
banner = strdup(*array);
}
dbus_free_string_array(array);
}
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
renewal = now + BANNER_RENEWAL_INTERVAL;
}
void PatchCord::invokeCommand(const char* command)
@ -112,9 +130,6 @@ void PatchCord::invokeCommand(const char* command)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
}
void PatchCord::setSpecificValue(uint32_t key, uint32_t value)
@ -138,9 +153,6 @@ void PatchCord::setSpecificValue(uint32_t key, uint32_t value)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
}
uint32_t PatchCord::getSpecificValue(uint32_t key)
@ -184,12 +196,8 @@ uint32_t PatchCord::getSpecificValue(uint32_t key)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
return value;
}
@ -240,7 +248,6 @@ uint32_t PatchCord::getPrivateIDForCall(const char* call)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
return value;
@ -297,7 +304,6 @@ bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
return result;

View file

@ -3,6 +3,7 @@
#ifndef PATCHCORDPROXY_H
#define PATCHCORDPROXY_H
#include <time.h>
#include <stdint.h>
#include <dbus/dbus.h>
@ -30,6 +31,7 @@ class PatchCord
char* name;
uint32_t number;
time_t renewal;
char* banner;
void getContextBanner();