On Sun, Apr 11, 2010 at 2:21 PM, Øyvind Harboe <[email protected]> wrote: > On Sat, Apr 10, 2010 at 6:55 PM, Antonio Borneo > <[email protected]> wrote: >> Hi, >> in "src/helper/membuf.c" there are several unused functions. >> >> I was working at a cleanup, and I noticed: >> - functions in "membuf.c" just put string messages in a buffer, to be >> eventually printed later. >> - such functions are only referenced in "src/flash/nor/at91sam3.c". >> - buffer is printed in "at91sam3.c" through command_print() or LOG_INFO(). >> - one of the unused function is the one that free() the buffer. This >> means in "at91sam3.c" there are buffers that are never freed. >> >> I believe would be better to revisit the file "at91sam3.c" and replace >> all usage of "membuf.c" with direct prints through command_print() or >> LOG_INFO(). >> After that, the whole helper file "membuf.c" could be removed. >> Any comment? > > Sounds great! > > I'm a great fan of deleting code and moving "general" functions > that are only used once and never will be used anywhere else > into the specific module to reduce interface surface area between > modules. Makes the whole system easier to understand and maintain. > >> I'm available to make this job, but I have no possibility to test the >> code, since missing the proper target board. >> Anyone available to contribute? > > If you break it apart in clear patches, then this seems like the > sort of thing we can clean up and have retested during > the release cycle. If a target is never tested, then the policy > is to apply cleanups regardless and possibly even remove > the specific target/flash support entirely.
Hi, in attachment 2 patches: - 0001... removes from "src/flash/nor/at91sam3.c" every reference to the helper functions in membuf.c - 0002... removes the (now) unused files membuf.c and membuf.h The system compiles properly, but I do not have the HW to test the functionality. Please run some test with the commands "flash info #" and "flash probe #" (where "#" can be "0" or, sometimes, "1") and verify you get printed all the information as before. You could get something printed in different order if you run "debug_level 3". Current code prints directly LOG_DEBUG() while put in a buffer the rest of output to print it later. With my patch, the output is printed directly, interleaved with debug messages. Best Regards, Antonio Borneo
From b82aa7b04bdceacdb68ad7efd8b607c5402ae46e Mon Sep 17 00:00:00 2001 From: Antonio Borneo <[email protected]> Date: Mon, 14 Jun 2010 12:22:10 +0800 Subject: [PATCH] nor/at91sam3: replace helper membuf Helper ./src/helper/membuf.c is only used in at91sam3.c 1) Replace membuf with LOG_* 2) The original code in sam3_GetDetails() invalidates all the buffered output of sam3_GetInfo(). The new code skips sam3_GetInfo() if its output should not be printed. Signed-off-by: Antonio Borneo <[email protected]> --- src/flash/nor/at91sam3.c | 126 ++++++++++++++------------------------------- 1 files changed, 39 insertions(+), 87 deletions(-) diff --git a/src/flash/nor/at91sam3.c b/src/flash/nor/at91sam3.c index 06b84cd..8fa6dd4 100644 --- a/src/flash/nor/at91sam3.c +++ b/src/flash/nor/at91sam3.c @@ -59,7 +59,6 @@ #include "imp.h" #include "at91sam3.h" -#include <helper/membuf.h> #include <helper/time_support.h> #define REG_NAME_WIDTH (12) @@ -211,8 +210,6 @@ struct sam3_chip { struct sam3_chip_details details; struct target *target; struct sam3_cfg cfg; - - struct membuf *mbuf; }; @@ -1000,20 +997,6 @@ FLASHD_Lock(struct sam3_bank_private *pPrivate, /****** END SAM3 CODE ********/ /* begin helpful debug code */ - -static void -sam3_sprintf(struct sam3_chip *pChip , const char *fmt, ...) -{ - va_list ap; - va_start(ap,fmt); - if (pChip->mbuf == NULL) { - return; - } - - membuf_vsprintf(pChip->mbuf, fmt, ap); - va_end(ap); -} - // print the fieldname, the field value, in dec & hex, and return field value static uint32_t sam3_reg_fieldname(struct sam3_chip *pChip, @@ -1038,7 +1021,7 @@ sam3_reg_fieldname(struct sam3_chip *pChip, } // show the basics - sam3_sprintf(pChip, "\t%*s: %*d [0x%0*x] ", + LOG_USER_N("\t%*s: %*d [0x%0*x] ", REG_NAME_WIDTH, regname, dwidth, v, hwidth, v); @@ -1173,16 +1156,16 @@ sam3_explain_ckgr_mor(struct sam3_chip *pChip) uint32_t rcen; v = sam3_reg_fieldname(pChip, "MOSCXTEN", pChip->cfg.CKGR_MOR, 0, 1); - sam3_sprintf(pChip, "(main xtal enabled: %s)\n", + LOG_USER_N("(main xtal enabled: %s)\n", _yes_or_no(v)); v = sam3_reg_fieldname(pChip, "MOSCXTBY", pChip->cfg.CKGR_MOR, 1, 1); - sam3_sprintf(pChip, "(main osc bypass: %s)\n", + LOG_USER_N("(main osc bypass: %s)\n", _yes_or_no(v)); rcen = sam3_reg_fieldname(pChip, "MOSCRCEN", pChip->cfg.CKGR_MOR, 2, 1); - sam3_sprintf(pChip, "(onchip RC-OSC enabled: %s)\n", + LOG_USER_N("(onchip RC-OSC enabled: %s)\n", _yes_or_no(rcen)); v = sam3_reg_fieldname(pChip, "MOSCRCF", pChip->cfg.CKGR_MOR, 4, 3); - sam3_sprintf(pChip, "(onchip RC-OSC freq: %s)\n", + LOG_USER_N("(onchip RC-OSC freq: %s)\n", _rc_freq[v]); pChip->cfg.rc_freq = 0; @@ -1203,14 +1186,14 @@ sam3_explain_ckgr_mor(struct sam3_chip *pChip) } v = sam3_reg_fieldname(pChip,"MOSCXTST", pChip->cfg.CKGR_MOR, 8, 8); - sam3_sprintf(pChip, "(startup clks, time= %f uSecs)\n", + LOG_USER_N("(startup clks, time= %f uSecs)\n", ((float)(v * 1000000)) / ((float)(pChip->cfg.slow_freq))); v = sam3_reg_fieldname(pChip, "MOSCSEL", pChip->cfg.CKGR_MOR, 24, 1); - sam3_sprintf(pChip, "(mainosc source: %s)\n", + LOG_USER_N("(mainosc source: %s)\n", v ? "external xtal" : "internal RC"); v = sam3_reg_fieldname(pChip,"CFDEN", pChip->cfg.CKGR_MOR, 25, 1); - sam3_sprintf(pChip, "(clock failure enabled: %s)\n", + LOG_USER_N("(clock failure enabled: %s)\n", _yes_or_no(v)); } @@ -1224,19 +1207,19 @@ sam3_explain_chipid_cidr(struct sam3_chip *pChip) const char *cp; sam3_reg_fieldname(pChip, "Version", pChip->cfg.CHIPID_CIDR, 0, 5); - sam3_sprintf(pChip,"\n"); + LOG_USER_N("\n"); v = sam3_reg_fieldname(pChip, "EPROC", pChip->cfg.CHIPID_CIDR, 5, 3); - sam3_sprintf(pChip, "%s\n", eproc_names[v]); + LOG_USER_N("%s\n", eproc_names[v]); v = sam3_reg_fieldname(pChip, "NVPSIZE", pChip->cfg.CHIPID_CIDR, 8, 4); - sam3_sprintf(pChip, "%s\n", nvpsize[v]); + LOG_USER_N("%s\n", nvpsize[v]); v = sam3_reg_fieldname(pChip, "NVPSIZE2", pChip->cfg.CHIPID_CIDR, 12, 4); - sam3_sprintf(pChip, "%s\n", nvpsize2[v]); + LOG_USER_N("%s\n", nvpsize2[v]); v = sam3_reg_fieldname(pChip, "SRAMSIZE", pChip->cfg.CHIPID_CIDR, 16,4); - sam3_sprintf(pChip, "%s\n", sramsize[ v ]); + LOG_USER_N("%s\n", sramsize[ v ]); v = sam3_reg_fieldname(pChip, "ARCH", pChip->cfg.CHIPID_CIDR, 20, 8); cp = _unknown; @@ -1247,13 +1230,13 @@ sam3_explain_chipid_cidr(struct sam3_chip *pChip) } } - sam3_sprintf(pChip, "%s\n", cp); + LOG_USER_N("%s\n", cp); v = sam3_reg_fieldname(pChip, "NVPTYP", pChip->cfg.CHIPID_CIDR, 28, 3); - sam3_sprintf(pChip, "%s\n", nvptype[ v ]); + LOG_USER_N("%s\n", nvptype[ v ]); v = sam3_reg_fieldname(pChip, "EXTID", pChip->cfg.CHIPID_CIDR, 31, 1); - sam3_sprintf(pChip, "(exists: %s)\n", _yes_or_no(v)); + LOG_USER_N("(exists: %s)\n", _yes_or_no(v)); } static void @@ -1263,14 +1246,14 @@ sam3_explain_ckgr_mcfr(struct sam3_chip *pChip) v = sam3_reg_fieldname(pChip, "MAINFRDY", pChip->cfg.CKGR_MCFR, 16, 1); - sam3_sprintf(pChip, "(main ready: %s)\n", _yes_or_no(v)); + LOG_USER_N("(main ready: %s)\n", _yes_or_no(v)); v = sam3_reg_fieldname(pChip, "MAINF", pChip->cfg.CKGR_MCFR, 0, 16); v = (v * pChip->cfg.slow_freq) / 16; pChip->cfg.mainosc_freq = v; - sam3_sprintf(pChip, "(%3.03f Mhz (%d.%03dkhz slowclk)\n", + LOG_USER_N("(%3.03f Mhz (%d.%03dkhz slowclk)\n", _tomhz(v), pChip->cfg.slow_freq / 1000, pChip->cfg.slow_freq % 1000); @@ -1283,17 +1266,17 @@ sam3_explain_ckgr_plla(struct sam3_chip *pChip) uint32_t mula,diva; diva = sam3_reg_fieldname(pChip, "DIVA", pChip->cfg.CKGR_PLLAR, 0, 8); - sam3_sprintf(pChip,"\n"); + LOG_USER_N("\n"); mula = sam3_reg_fieldname(pChip, "MULA", pChip->cfg.CKGR_PLLAR, 16, 11); - sam3_sprintf(pChip,"\n"); + LOG_USER_N("\n"); pChip->cfg.plla_freq = 0; if (mula == 0) { - sam3_sprintf(pChip,"\tPLLA Freq: (Disabled,mula = 0)\n"); + LOG_USER_N("\tPLLA Freq: (Disabled,mula = 0)\n"); } else if (diva == 0) { - sam3_sprintf(pChip,"\tPLLA Freq: (Disabled,diva = 0)\n"); + LOG_USER_N("\tPLLA Freq: (Disabled,diva = 0)\n"); } else if (diva == 1) { pChip->cfg.plla_freq = (pChip->cfg.mainosc_freq * (mula + 1)); - sam3_sprintf(pChip,"\tPLLA Freq: %3.03f MHz\n", + LOG_USER_N("\tPLLA Freq: %3.03f MHz\n", _tomhz(pChip->cfg.plla_freq)); } } @@ -1334,7 +1317,7 @@ sam3_explain_mckr(struct sam3_chip *pChip) break; } - sam3_sprintf(pChip, "%s (%3.03f Mhz)\n", + LOG_USER_N("%s (%3.03f Mhz)\n", cp, _tomhz(fin)); pres = sam3_reg_fieldname(pChip, "PRES", pChip->cfg.PMC_MCKR, 4, 3); @@ -1374,14 +1357,14 @@ sam3_explain_mckr(struct sam3_chip *pChip) assert(0); break; } - sam3_sprintf(pChip, "(%s)\n", cp); + LOG_USER_N("(%s)\n", cp); fin = fin / pdiv; // sam3 has a *SINGLE* clock - // other at91 series parts have divisors for these. pChip->cfg.cpu_freq = fin; pChip->cfg.mclk_freq = fin; pChip->cfg.fclk_freq = fin; - sam3_sprintf(pChip, "\t\tResult CPU Freq: %3.03f\n", + LOG_USER_N("\t\tResult CPU Freq: %3.03f\n", _tomhz(fin)); } @@ -1538,15 +1521,12 @@ sam3_GetInfo(struct sam3_chip *pChip) const struct sam3_reg_list *pReg; uint32_t regval; - membuf_reset(pChip->mbuf); - - pReg = &(sam3_all_regs[0]); while (pReg->name) { // display all regs LOG_DEBUG("Start: %s", pReg->name); regval = *sam3_get_reg_ptr(&(pChip->cfg), pReg); - sam3_sprintf(pChip, "%*s: [0x%08x] -> 0x%08x\n", + LOG_USER_N("%*s: [0x%08x] -> 0x%08x\n", REG_NAME_WIDTH, pReg->name, pReg->address, @@ -1557,14 +1537,14 @@ sam3_GetInfo(struct sam3_chip *pChip) LOG_DEBUG("End: %s", pReg->name); pReg++; } - sam3_sprintf(pChip," rc-osc: %3.03f MHz\n", _tomhz(pChip->cfg.rc_freq)); - sam3_sprintf(pChip," mainosc: %3.03f MHz\n", _tomhz(pChip->cfg.mainosc_freq)); - sam3_sprintf(pChip," plla: %3.03f MHz\n", _tomhz(pChip->cfg.plla_freq)); - sam3_sprintf(pChip," cpu-freq: %3.03f MHz\n", _tomhz(pChip->cfg.cpu_freq)); - sam3_sprintf(pChip,"mclk-freq: %3.03f MHz\n", _tomhz(pChip->cfg.mclk_freq)); + LOG_USER_N(" rc-osc: %3.03f MHz\n", _tomhz(pChip->cfg.rc_freq)); + LOG_USER_N(" mainosc: %3.03f MHz\n", _tomhz(pChip->cfg.mainosc_freq)); + LOG_USER_N(" plla: %3.03f MHz\n", _tomhz(pChip->cfg.plla_freq)); + LOG_USER_N(" cpu-freq: %3.03f MHz\n", _tomhz(pChip->cfg.cpu_freq)); + LOG_USER_N("mclk-freq: %3.03f MHz\n", _tomhz(pChip->cfg.mclk_freq)); - sam3_sprintf(pChip, " UniqueId: 0x%08x 0x%08x 0x%08x 0x%08x\n", + LOG_USER_N(" UniqueId: 0x%08x 0x%08x 0x%08x 0x%08x\n", pChip->cfg.unique_id[0], pChip->cfg.unique_id[1], pChip->cfg.unique_id[2], @@ -1664,11 +1644,6 @@ FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command) // assumption is this runs at 32khz pChip->cfg.slow_freq = 32768; pChip->probed = 0; - pChip->mbuf = membuf_new(); - if (!(pChip->mbuf)) { - LOG_ERROR("no memory"); - return ERROR_FAIL; - } } switch (bank->base) { @@ -1702,11 +1677,8 @@ sam3_GetDetails(struct sam3_bank_private *pPrivate) { const struct sam3_chip_details *pDetails; struct sam3_chip *pChip; - void *vp; struct flash_bank *saved_banks[SAM3_MAX_FLASH_BANKS]; - unsigned x; - const char *cp; LOG_DEBUG("Begin"); pDetails = all_sam3_details; @@ -1721,16 +1693,9 @@ sam3_GetDetails(struct sam3_bank_private *pPrivate) LOG_ERROR("SAM3 ChipID 0x%08x not found in table (perhaps you can this chip?)", (unsigned int)(pPrivate->pChip->cfg.CHIPID_CIDR)); // Help the victim, print details about the chip - membuf_reset(pPrivate->pChip->mbuf); - membuf_sprintf(pPrivate->pChip->mbuf, - "SAM3 CHIPID_CIDR: 0x%08x decodes as follows\n", + LOG_INFO_N("SAM3 CHIPID_CIDR: 0x%08x decodes as follows\n", pPrivate->pChip->cfg.CHIPID_CIDR); sam3_explain_chipid_cidr(pPrivate->pChip); - cp = membuf_strtok(pPrivate->pChip->mbuf, "\n", &vp); - while (cp) { - LOG_INFO("%s", cp); - cp = membuf_strtok(NULL, "\n", &vp); - } return ERROR_FAIL; } @@ -1798,18 +1763,14 @@ _sam3_probe(struct flash_bank *bank, int noise) LOG_DEBUG("Here"); - r = sam3_GetInfo(pPrivate->pChip); + if (pPrivate->pChip->probed) { + r = sam3_GetInfo(pPrivate->pChip); + } else { + r = sam3_GetDetails(pPrivate); + } if (r != ERROR_OK) { return r; } - if (!(pPrivate->pChip->probed)) { - pPrivate->pChip->probed = 1; - LOG_DEBUG("Here"); - r = sam3_GetDetails(pPrivate); - if (r != ERROR_OK) { - return r; - } - } // update the flash bank size for (x = 0 ; x < SAM3_MAX_FLASH_BANKS ; x++) { @@ -2256,8 +2217,6 @@ sam3_write(struct flash_bank *bank, COMMAND_HANDLER(sam3_handle_info_command) { struct sam3_chip *pChip; - void *vp; - const char *cp; unsigned x; int r; @@ -2319,13 +2278,6 @@ COMMAND_HANDLER(sam3_handle_info_command) return r; } - - // print results - cp = membuf_strtok(pChip->mbuf, "\n", &vp); - while (cp) { - command_print(CMD_CTX,"%s", cp); - cp = membuf_strtok(NULL, "\n", &vp); - } return ERROR_OK; } -- 1.5.2.2
From e64a1a44a3504819d74f1d0fe139ee9f524e7c53 Mon Sep 17 00:00:00 2001 From: Antonio Borneo <[email protected]> Date: Mon, 14 Jun 2010 12:31:13 +0800 Subject: [PATCH] helper: remove helper membuf Helper is now unused. Can be removed. Signed-off-by: Antonio Borneo <[email protected]> --- src/helper/Makefile.am | 4 +- src/helper/membuf.c | 240 ------------------------------------------------ src/helper/membuf.h | 137 --------------------------- 3 files changed, 1 insertions(+), 380 deletions(-) diff --git a/src/helper/Makefile.am b/src/helper/Makefile.am index 3ec4f31..27ae1e2 100644 --- a/src/helper/Makefile.am +++ b/src/helper/Makefile.am @@ -22,8 +22,7 @@ libhelper_la_SOURCES = \ command.c \ time_support.c \ replacements.c \ - fileio.c \ - membuf.c + fileio.c if IOUTIL libhelper_la_SOURCES += ioutil.c @@ -44,7 +43,6 @@ noinst_HEADERS = \ types.h \ log.h \ command.h \ - membuf.h \ time_support.h \ replacements.h \ fileio.h \ diff --git a/src/helper/membuf.c b/src/helper/membuf.c deleted file mode 100644 index 766364a..0000000 --- a/src/helper/membuf.c +++ /dev/null @@ -1,240 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009 By Duane Ellis * - * [email protected] * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#include <stdio.h> -#include <stdarg.h> -#include <stdlib.h> -#include <string.h> - -#include "membuf.h" - -struct membuf { - // buflen is alway "+1" bigger then - // what is shown here, the +1 is for - // the NULL string terminator -#define DEFAULT_BUFSIZE 100 - size_t maxlen; // allocated size - size_t curlen; // where we are inserting at - char *_strtoklast; - void *buf; -}; - - -#define space_avail(pBuf) (pBuf->maxlen - pBuf->curlen) -#define dataend(pBuf) (((char *)(pBuf->buf)) + pBuf->curlen) - -size_t -membuf_len(struct membuf *pBuf) -{ - return pBuf->curlen; -} - -const void * -membuf_datapointer(struct membuf *pBuf) -{ - return ((void *)(pBuf->buf)); -} - -const char * -membuf_strtok(struct membuf *pBuf, const char *sep, void **pLast) -{ - if (pBuf) { - pBuf->_strtoklast = NULL; - *pLast = pBuf; - // this should be "strtok_r()" but windows lacks */ - return strtok(((char *)(pBuf->buf)), sep); - } else { - // recover our pBuf - pBuf = *((struct membuf **)(pLast)); - // this should be "strtok_r()" but windows lacks */ - return strtok( NULL, sep); - } -} - - - -struct membuf * -membuf_new(void) -{ - // by default - parameters are zero. - struct membuf *pBuf; - - pBuf = calloc(1, sizeof(*pBuf)); - if (pBuf) { - // we *ALWAYS* allocate +1 for null terminator. - pBuf->buf = calloc(DEFAULT_BUFSIZE + 1, sizeof(char)); - if (pBuf->buf == NULL) { - free(pBuf); - pBuf = NULL; - } else { - pBuf->maxlen = DEFAULT_BUFSIZE; - } - } - return pBuf; -} - - -struct membuf * -membuf_grow(struct membuf *pBuf, int n) -{ - void *vp; - signed int newsize; - - // this is a *SIGNED* value - newsize = ((int)(pBuf->maxlen)) + n; - - // do not go negative, or too small - if (newsize < DEFAULT_BUFSIZE) { - newsize = DEFAULT_BUFSIZE; - } - - // always alloc +1 for the null terminator - vp = realloc(pBuf->buf, newsize + 1); - if (vp) { - pBuf->buf = vp; - pBuf->maxlen = newsize; - return pBuf; - } else { - return NULL; - } -} - - -void membuf_reset(struct membuf *pBuf) -{ - pBuf->curlen = 0; -} - - -void membuf_delete(struct membuf *pBuf) -{ - if (pBuf) { - if (pBuf->buf) { - // wack data so it cannot be reused - memset(pBuf->buf,0,pBuf->maxlen); - free(pBuf->buf); - } - // wack dat so it cannot be reused - memset(pBuf,0,sizeof(pBuf)); - free(pBuf); - } -} - -int -membuf_sprintf(struct membuf *pBuf , const char *fmt, ...) -{ - int r; - va_list ap; - va_start(ap, fmt); - r = membuf_vsprintf(pBuf, fmt, ap); - va_end(ap); - return r; -} - -int -membuf_vsprintf(struct membuf *pBuf, const char *fmt, va_list ap) -{ - int r; - size_t sa; - int grew; - - - grew = 0; - for (;;) { - sa = space_avail(pBuf); - - // do work - r = vsnprintf(dataend(pBuf), - sa, - fmt, - ap); - if ((r > 0) && (((size_t)(r)) < sa)) { - // Success! - pBuf->curlen += ((size_t)(r)); - // remember: We always alloc'ed +1 - // so this does not overflow - ((char *)(pBuf->buf))[ pBuf->curlen ] = 0; - r = 0; - break; - } - - // failure - if (r < 0) { - // Option(A) format error - // Option(B) glibc2.0 bug - // assume (B). - r = (4 * DEFAULT_BUFSIZE); - } - - // don't do this again - if (grew) { - r = -1; - break; - } - grew = 1; - pBuf = membuf_grow(pBuf, r); - if (pBuf == NULL) { - // grow failed - r = -1; - break; - } - } - return r; -} - -struct membuf * -membuf_strcat(struct membuf *pBuf, const char *pStr) -{ - return membuf_append(pBuf, pStr, strlen(pStr)); -} - -struct membuf * -membuf_append(struct membuf *pBuf, const void *pData, size_t len) -{ - size_t sa; - int r; - - // how much room is there? - sa = space_avail(pBuf); - - // will it fit? - if (sa < len) { - // if not, how much do we need? - r = ((int)(sa - len)); - // do the grow. - pBuf = membuf_grow(pBuf, r); - // failed? - if (pBuf == NULL) { - return pBuf; - } - } - // append - memcpy(dataend(pBuf), - pData, - len); - pBuf->curlen += len; - return pBuf; -} - - - - - - diff --git a/src/helper/membuf.h b/src/helper/membuf.h deleted file mode 100644 index fa96998..0000000 --- a/src/helper/membuf.h +++ /dev/null @@ -1,137 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009 By Duane Ellis * - * [email protected] * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -#ifndef HELPER_MEMBUF_H -#define HELPER_MEMBUF_H - -/** @file - * MEMBUF - an auto-growing string buffer - * - * With OpenOCD often, one must write code that sends text to - * different places.. the historical command_ctx, or JIM output, - * and/or other places. - * - * This is a simple 'string buffer' that auto-grows. - * - * More correctly put, this is a "memory buffer" - * it may contain binary data - * - * Note: Internally the buffer always has a 'null terminator' - */ - -/* contents of this structure are 'opaque' */ -struct membuf; - - -/** Create a new membuf - * By default the memory buffer has "some non-zero-size" - * (couple hundred bytes, exact amount is opaque) - */ -struct membuf *membuf_new(void); - -/** delete (destroy) the mem buffer - * @param pBuf - buffer to release - */ -void membuf_delete(struct membuf *pBuf); - - -/** grow/shrink a membuf by specified amount. - * @param pBuf - the buffer - * @param amount - the amount to grow or shrink by. - * - * Symantics of 'realloc()' return NULL on failure - */ -struct membuf *membuf_grow(struct membuf *pBuf, int amount); - -/** how long is this buffer (memlen(), strlen()) - * @param pBuf - the buffer - * - * @returns: length of current buffer. - */ -size_t membuf_len(struct membuf *pBuf); - - -/** reset an membuf to zero length. - * @param pBuf - buffer to reset - * - * Note this does not 'release' the memory buffer - */ -void membuf_reset(struct membuf *pBuf); - - -/** sprintf() to the string buffer - * @param pBuf - buffer to capture sprintf() data into - * @param fmt - printf format - * - * Returns 0 on success - * Returns non-zero on failure - */ -int membuf_sprintf(struct membuf *pBuf , const char *fmt, ...); - -/** vsprintf() to the string buffer - * @param pBuf - buffer to capture sprintf() data into - * @param fmt - printf format - * @param ap - va_list for fmt - * - * Returns 0 on success - * Returns non-zero on failure - */ -int membuf_vsprintf(struct membuf *pBuf , const char *fmt, va_list ap); - -/** Tokenize lines using strtok() - * @param pBuf - buffer to tokenize - * @param delim - delimiter parameter for strtok_r() - * @param pSave - pointer to string context for tokenization - * - * Identical to "strtok()" - pass "pBuff = NULL" on second call - * - * NOTE: This call is <b > destructive</b> to the buffer. - */ -const char *membuf_strtok(struct membuf *pBuf, const char *delim, void **pSave); - -/** Return pointer to the memory in the buffer - * @param pBuf - buffer - * - * NOTE: Thou shall not modify this pointer, it is <b > CONST</b> - */ -const void *membuf_datapointer(struct membuf *pBuf); - - -/** Append data to the buffer - * @param pBuf - buffer to append - * @param pData - pointer to data to append - * @param len - length of data to append - * - * Modified symantics of "memcpy()". On memory allocation failure - * returns NULL. On success, returns pointer to orginal membuf. - */ -struct membuf *membuf_append(struct membuf *pBuf, const void *pData, size_t len); - - -/** Append string to the buffer - * @param pBuf - buffer to append - * @param str - string to append - * - * Modified symantics of "strcat()". On memory allocation failure - * returns NULL. On success, returns pointer to orginal membuf. - */ -struct membuf *membuf_strcat(struct membuf *pBuf, const char *str); - - -#endif -- 1.5.2.2
_______________________________________________ Openocd-development mailing list [email protected] https://lists.berlios.de/mailman/listinfo/openocd-development
