Browse Source

fix some potential memory leaks, remove a few old unused functions

djm34
Jan Berdajs 11 years ago
parent
commit
e4a4efdb42
  1. 4
      driver-opencl.c
  2. 3
      findnonce.c
  3. 20
      miner.h
  4. 59
      ocl.c
  5. 15
      sgminer.c
  6. 43
      util.c
  7. 1
      util.h

4
driver-opencl.c

@ -1294,6 +1294,8 @@ static bool opencl_thread_init(struct thr_info *thr) @@ -1294,6 +1294,8 @@ static bool opencl_thread_init(struct thr_info *thr)
status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
buffersize, blank_res, 0, NULL, NULL);
if (unlikely(status != CL_SUCCESS)) {
free(thrdata->res);
free(thrdata);
applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
return false;
}
@ -1434,6 +1436,8 @@ static void opencl_thread_shutdown(struct thr_info *thr) @@ -1434,6 +1436,8 @@ static void opencl_thread_shutdown(struct thr_info *thr)
clReleaseProgram(clState->program);
clReleaseCommandQueue(clState->commandQueue);
clReleaseContext(clState->context);
if (clState->extra_kernels)
free(clState->extra_kernels);
free(clState);
}
}

3
findnonce.c

@ -230,6 +230,7 @@ void postcalc_hash_async(struct thr_info *thr, struct work *work, uint32_t *res) @@ -230,6 +230,7 @@ void postcalc_hash_async(struct thr_info *thr, struct work *work, uint32_t *res)
if (pthread_create(&pcd->pth, NULL, postcalc_hash, (void *)pcd)) {
applog(LOG_ERR, "Failed to create postcalc_hash thread");
return;
discard_work(pcd->work);
free(pcd);
}
}

20
miner.h

@ -370,8 +370,6 @@ struct device_drv { @@ -370,8 +370,6 @@ struct device_drv {
double working_diff;
};
extern struct device_drv *copy_drv(struct device_drv*);
enum dev_enable {
DEV_ENABLED,
DEV_DISABLED,
@ -573,24 +571,6 @@ struct string_elist { @@ -573,24 +571,6 @@ struct string_elist {
struct list_head list;
};
static inline void string_elist_add(const char *s, struct list_head *head)
{
struct string_elist *n;
n = (struct string_elist *)calloc(1, sizeof(*n));
n->string = strdup(s);
n->free_me = true;
list_add_tail(&n->list, head);
}
static inline void string_elist_del(struct string_elist *item)
{
if (item->free_me)
free(item->string);
list_del(&item->list);
}
static inline uint32_t swab32(uint32_t v)
{
return bswap_32(v);

59
ocl.c

@ -278,7 +278,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg @@ -278,7 +278,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg
}
if (numDevices > 0 ) {
devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
devices = (cl_device_id *)alloca(numDevices*sizeof(cl_device_id));
/* Now, get the device list data */
@ -339,7 +339,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg @@ -339,7 +339,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg
/* Check for BFI INT support. Hopefully people don't mix devices with
* and without it! */
char * extensions = (char *)malloc(1024);
char extensions[1024];
const char * camo = "cl_amd_media_ops";
char *find;
@ -353,7 +353,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg @@ -353,7 +353,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg
clState->hasBitAlign = true;
/* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
char * devoclver = (char *)malloc(1024);
char devoclver[1024];
const char * ocl10 = "OpenCL 1.0";
const char * ocl11 = "OpenCL 1.1";
@ -493,6 +493,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg @@ -493,6 +493,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg
binaries = (char **)calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
if (unlikely(!binaries)) {
applog(LOG_ERR, "Unable to calloc binaries");
free(binary_sizes);
return NULL;
}
@ -528,7 +529,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg @@ -528,7 +529,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize, algorithm_t *alg
if (unlikely(!binaries[slot])) {
applog(LOG_ERR, "Unable to calloc binaries");
fclose(binaryfile);
return NULL;
goto not_built;
}
if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
@ -562,7 +563,7 @@ build: @@ -562,7 +563,7 @@ build:
clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
return NULL;
goto not_built;
}
/* create a cl program executable for all the devices specified */
@ -615,13 +616,13 @@ build: @@ -615,13 +616,13 @@ build:
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
size_t logSize;
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
size_t log_size;
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
char *log = (char *)malloc(logSize);
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
applog(LOG_ERR, "%s", log);
return NULL;
char sz_log[log_size];
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, log_size, sz_log, NULL);
applog(LOG_ERR, "%s", sz_log);
goto not_built;
}
prog_built = true;
@ -635,13 +636,13 @@ build: @@ -635,13 +636,13 @@ build:
status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
if (unlikely(status != CL_SUCCESS)) {
applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
return NULL;
goto not_built;
}
status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
if (unlikely(status != CL_SUCCESS)) {
applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
return NULL;
goto not_built;
}
/* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
@ -655,13 +656,13 @@ build: @@ -655,13 +656,13 @@ build:
applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, (int)(binary_sizes[slot]));
if (!binary_sizes[slot]) {
applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
return NULL;
goto not_built;
}
binaries[slot] = (char *)calloc(sizeof(char)* binary_sizes[slot], 1);
status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
if (unlikely(status != CL_SUCCESS)) {
applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
return NULL;
goto not_built;
}
/* Patch the kernel if the hardware supports BFI_INT but it needs to
@ -701,13 +702,13 @@ build: @@ -701,13 +702,13 @@ build:
status = clReleaseProgram(clState->program);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
return NULL;
goto not_built;
}
clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
return NULL;
goto not_built;
}
/* Program needs to be rebuilt */
@ -724,10 +725,18 @@ build: @@ -724,10 +725,18 @@ build:
} else {
if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
applog(LOG_ERR, "Unable to fwrite to binaryfile");
return NULL;
}
fclose(binaryfile);
}
goto built;
not_built:
if (binaries[slot])
free(binaries[slot]);
free(binaries);
free(binary_sizes);
return NULL;
built:
if (binaries[slot])
free(binaries[slot]);
@ -743,12 +752,12 @@ built: @@ -743,12 +752,12 @@ built:
status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
size_t logSize;
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
size_t log_size;
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
char *log = (char *)malloc(logSize);
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
applog(LOG_ERR, "%s", log);
char sz_log[log_size];
status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, log_size, sz_log, NULL);
applog(LOG_ERR, "%s", sz_log);
return NULL;
}
}
@ -764,7 +773,7 @@ built: @@ -764,7 +773,7 @@ built:
clState->n_extra_kernels = algorithm->n_extra_kernels;
if (clState->n_extra_kernels > 0) {
unsigned int i;
char *kernel_name = (char *)malloc(9); // max: search99 + 0x0
char kernel_name[9]; // max: search99 + 0x0
clState->extra_kernels = (cl_kernel *)malloc(sizeof(cl_kernel) * clState->n_extra_kernels);
@ -776,8 +785,6 @@ built: @@ -776,8 +785,6 @@ built:
return NULL;
}
}
free(kernel_name);
}
size_t bufsize;

15
sgminer.c

@ -1673,6 +1673,7 @@ static char *load_config(const char *arg, void __maybe_unused *unused) @@ -1673,6 +1673,7 @@ static char *load_config(const char *arg, void __maybe_unused *unused)
#endif
if (!json_is_object(config)) {
siz = JSON_LOAD_ERROR_LEN + strlen(arg) + strlen(err.text);
// TODO: memory leak
json_error = (char *)malloc(siz);
if (!json_error)
quit(1, "Malloc failure in json error");
@ -5698,6 +5699,7 @@ static void *stratum_sthread(void *userdata) @@ -5698,6 +5699,7 @@ static void *stratum_sthread(void *userdata)
continue;
}
// TODO: check for memory leaks
sshare = (struct stratum_share *)calloc(sizeof(struct stratum_share), 1);
hash32 = (uint32_t *)work->hash;
submitted = false;
@ -8090,19 +8092,6 @@ bool add_cgpu(struct cgpu_info *cgpu) @@ -8090,19 +8092,6 @@ bool add_cgpu(struct cgpu_info *cgpu)
return true;
}
struct device_drv *copy_drv(struct device_drv *drv)
{
struct device_drv *copy;
if (unlikely(!(copy = (struct device_drv *)malloc(sizeof(*copy))))) {
quit(1, "Failed to allocate device_drv copy of %s (%s)",
drv->name, drv->copy ? "copy" : "original");
}
memcpy(copy, drv, sizeof(*copy));
copy->copy = true;
return copy;
}
static void probe_pools(void)
{
int i;

43
util.c

@ -2641,40 +2641,6 @@ void *realloc_strcat(char *ptr, char *s) @@ -2641,40 +2641,6 @@ void *realloc_strcat(char *ptr, char *s)
return ret;
}
/* Make a text readable version of a string using 0xNN for < ' ' or > '~'
* Including 0x00 at the end
* You must free the result yourself */
void *str_text(char *ptr)
{
unsigned char *uptr;
char *ret, *txt;
if (ptr == NULL) {
ret = strdup("(null)");
if (unlikely(!ret))
quithere(1, "Failed to malloc null");
}
uptr = (unsigned char *)ptr;
ret = txt = (char *)malloc(strlen(ptr) * 4 + 5); // Guaranteed >= needed
if (unlikely(!txt))
quithere(1, "Failed to malloc txt");
do {
if (*uptr < ' ' || *uptr > '~') {
sprintf(txt, "0x%02x", *uptr);
txt += 4;
} else
*(txt++) = *uptr;
} while (*(uptr++));
*txt = '\0';
return ret;
}
void RenameThread(const char* name)
{
char buf[16];
@ -2900,10 +2866,11 @@ bool cg_completion_timeout(void *fn, void *fnarg, int timeout) @@ -2900,10 +2866,11 @@ bool cg_completion_timeout(void *fn, void *fnarg, int timeout)
pthread_create(&pthread, NULL, completion_thread, (void *)cgc);
ret = cgsem_mswait(&cgc->cgsem, timeout);
if (!ret) {
pthread_join(pthread, NULL);
free(cgc);
} else
if (ret)
pthread_cancel(pthread);
pthread_join(pthread, NULL);
free(cgc);
return !ret;
}

1
util.h

@ -143,7 +143,6 @@ bool restart_stratum(struct pool *pool); @@ -143,7 +143,6 @@ bool restart_stratum(struct pool *pool);
void suspend_stratum(struct pool *pool);
void dev_error(struct cgpu_info *dev, enum dev_reason reason);
void *realloc_strcat(char *ptr, char *s);
void *str_text(char *ptr);
void RenameThread(const char* name);
void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);

Loading…
Cancel
Save