From d1f4e5876b55ca740276185a5e8364390275172a Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 11 May 2016 16:31:51 +0200 Subject: [PATCH] refclock: avoid reallocation of refclock instances Change the array with refclock instances to store just pointers and avoid reallocation of the instances. This fixes a bug with the SOCK refclock, which uses the pointer to the instance in a file handler and which was invalid when the instance was reallocated (after adding another refclock). The bug is from commit d92583ed330f4c1f5f29fc1fc7c01d2a19d12319. --- refclock.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/refclock.c b/refclock.c index 59765f8..7ee1468 100644 --- a/refclock.c +++ b/refclock.c @@ -87,7 +87,7 @@ struct RCL_Instance_Record { SRC_Instance source; }; -/* Array of RCL_Instance_Record */ +/* Array of pointers to RCL_Instance_Record */ static ARR_Instance refclocks; static LOG_FileID logfileid; @@ -114,13 +114,13 @@ static void filter_add_dispersion(struct MedianFilter *filter, double dispersion static RCL_Instance get_refclock(unsigned int index) { - return (RCL_Instance)ARR_GetElement(refclocks, index); + return *(RCL_Instance *)ARR_GetElement(refclocks, index); } void RCL_Initialise(void) { - refclocks = ARR_CreateInstance(sizeof (struct RCL_Instance_Record)); + refclocks = ARR_CreateInstance(sizeof (RCL_Instance)); CNF_AddRefclocks(); @@ -148,6 +148,7 @@ RCL_Finalise(void) filter_fini(&inst->filter); Free(inst->driver_parameter); SRC_DestroyInstance(inst->source); + Free(inst); } if (ARR_GetSize(refclocks) > 0) { @@ -162,8 +163,10 @@ int RCL_AddRefclock(RefclockParameters *params) { int pps_source = 0; + RCL_Instance inst; - RCL_Instance inst = ARR_GetNewElement(refclocks); + inst = MallocNew(struct RCL_Instance_Record); + *(RCL_Instance *)ARR_GetNewElement(refclocks) = inst; if (strcmp(params->driver_name, "SHM") == 0) { inst->driver = &RCL_SHM_driver;