Skip to content

Commit

Permalink
revert to c memory
Browse files Browse the repository at this point in the history
  • Loading branch information
kriemo committed Jan 22, 2024
1 parent e50b2e9 commit dd5e76d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: raer
Type: Package
Title: RNA editing tools in R
Version: 1.1.1
Version: 1.1.2
Authors@R: c(
person("Kent", "Riemondy", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0750-1273")),
Expand Down
32 changes: 14 additions & 18 deletions src/regfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@

static inline void free_regidx(void* payload) {
payload_t* pld = *((payload_t**)payload);
if (pld->alt) R_Free(pld->alt);
if (pld->ref) R_Free(pld->ref);
R_Free(pld);
if (pld->alt) free(pld->alt);
if (pld->ref) free(pld->ref);
free(pld);
}

// use R memory handling for strdup
static inline char * rstrdup(const char *x) {
char *buf;
size_t l = strlen(x) + 1;
buf = R_Calloc(l, char);
strcpy(buf, x);
return buf;
}

static void load_payload(payload_t* pld, int strand, char* ref,
char* alt, int rowidx) {
pld->strand = strand;
pld->alt = rstrdup(alt);
pld->ref = rstrdup(ref);
pld->alt = strdup(alt);
pld->ref = strdup(ref);
pld->idx = rowidx;
}

Expand All @@ -39,12 +30,14 @@ static regidx_t* regidx_load_payload(char** chroms, int* pos, int* strand,
payload_t* pld;
for (i = 0; i < n_sites; ++i) {
chr_beg = chroms[i];
// use R memory management to avoid memory leak if index build has an error
pld = (payload_t*) R_Calloc(1, payload_t);
pld = (payload_t*) calloc(1, sizeof(payload_t));
load_payload(pld, strand[i], ref[i], alt[i], rowidx[i]);
hts_pos_t p = (hts_pos_t) pos[i] - 1; // convert 1 to 0 based
ret = regidx_push(idx, chr_beg, chr_beg + strlen(chr_beg) - 1, p, p, &pld);
if (ret < 0) Rf_error("[raer internal] index push failed\n");
if (ret < 0) {
if(idx) regidx_destroy(idx);
Rf_error("[raer internal] index push failed\n");
}
}
return idx;
}
Expand All @@ -61,7 +54,10 @@ static regidx_t* regidx_load_simple(char** chroms, int* start, int* end, int n_s
hts_pos_t s = (hts_pos_t) start[i] - 1; // convert to 0 based
hts_pos_t e = (hts_pos_t) end[i] - 1; // inclusive
ret = regidx_push(idx, chr_beg, chr_beg + strlen(chr_beg) - 1, s, e, NULL);
if (ret < 0) Rf_error("[raer internal] index push failed\n");
if (ret < 0) {
if(idx) regidx_destroy(idx);
Rf_error("[raer internal] index push failed\n");
}
}
return idx;
}
Expand Down

0 comments on commit dd5e76d

Please sign in to comment.