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

3
findnonce.c

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

15
sgminer.c

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

43
util.c

@ -2641,40 +2641,6 @@ void *realloc_strcat(char *ptr, char *s)
return ret; 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) void RenameThread(const char* name)
{ {
char buf[16]; char buf[16];
@ -2900,10 +2866,11 @@ bool cg_completion_timeout(void *fn, void *fnarg, int timeout)
pthread_create(&pthread, NULL, completion_thread, (void *)cgc); pthread_create(&pthread, NULL, completion_thread, (void *)cgc);
ret = cgsem_mswait(&cgc->cgsem, timeout); ret = cgsem_mswait(&cgc->cgsem, timeout);
if (!ret) {
pthread_join(pthread, NULL); if (ret)
free(cgc);
} else
pthread_cancel(pthread); pthread_cancel(pthread);
pthread_join(pthread, NULL);
free(cgc);
return !ret; return !ret;
} }

1
util.h

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

Loading…
Cancel
Save