Esempio n. 1
0
char * StrNew(const char * Str)
{
  rsize_t sz = strlen(Str) + 1;
  char * Result = static_cast<char *>(nb_malloc(sizeof(char) * sz));
  strncpy_s(Result, sz, Str, sz);
  return Result;
}
Esempio n. 2
0
void * TMemoryStream::Realloc(__int64 & NewCapacity)
{
  if ((NewCapacity > 0) && (NewCapacity != FSize))
  {
    NewCapacity = (NewCapacity + (MemoryDelta - 1)) & ~(MemoryDelta - 1);
  }
  void * Result = FMemory;
  if (NewCapacity != FCapacity)
  {
    if (NewCapacity == 0)
    {
      nb_free(FMemory);
      FMemory = nullptr;
      Result = nullptr;
    }
    else
    {
      if (FCapacity == 0)
      {
        Result = nb_malloc(static_cast<size_t>(NewCapacity));
      }
      else
      {
        Result = nb_realloc(FMemory, static_cast<size_t>(NewCapacity));
      }
      if (Result == nullptr)
      {
        throw EStreamError(FMTLOAD(SMemoryStreamError));
      }
    }
  }
  return Result;
}
Esempio n. 3
0
static int nb_config_readsz(struct nb_config *cfg, char **v) {
	struct tnt_tk *tk;
	if (nb_config_expect(cfg, TNT_TK_STRING, &tk) == -1)
		return -1;
	if (*v)
		free(*v);
	*v = nb_malloc(TNT_TK_S(tk)->size + 1);
	memcpy(*v, (char*)TNT_TK_S(tk)->data, TNT_TK_S(tk)->size + 1);
	return 0;
}
Esempio n. 4
0
void nb_statistics_report(struct nb_statistics *s, int workers, int tick)
{
	struct nb_stat_avg *n = nb_malloc(sizeof(struct nb_stat_avg));
	nb_statistics_current(s, n);
	n->workers = workers;
	n->time = tick;
	n->next = NULL;
	if (s->head == NULL)
		s->head = n;
	else
		s->tail->next = n;
	s->tail = n;
	s->count_report++;
	s->current = n;
}
Esempio n. 5
0
char *read_header (char *header, size_t *len, FILE *file)
{
  int next_char;

  header = read_line (header, len, file);
  if (!header)
    /* We reached EOF */
    return header;

  while (1)
  {
    next_char = getc (file);
    if (next_char == EOF)
      return header;

    ungetc (next_char, file);
    if (next_char != ' ' && next_char != '\t')
      return header;

    /* The current header is continued on the next line */
    {
      char *cont = nb_malloc (256);
      size_t contlen = 256;
      int space_needed;

      cont = read_line (cont, &contlen, file);
      if (!cont)
      {
	/* This cannot happen - we have at least the ungetted char to read */
	nb_error (0, _("internal error while reading a header continuation"));
      }

      space_needed = strlen (header) + strlen (cont) + 1;
      if (space_needed > *len)
      {
	header = nb_realloc (header, space_needed);
	*len = space_needed;
      }
      strcat (header, cont);
      free (cont);
    }
  }
}
Esempio n. 6
0
static void expand_argv (char *argv[], char *filename)
{
  char *argument;
  char *new_argument;
  char *c;
  int offset;

  while ((argument = *(++ argv)) != NULL)
  {
    offset = 0;
    while ((c = strchr (argument + offset, '%')) != NULL)
    {
      switch (*(c + 1))
      {
      case 'f':
	/* We have a menory leak here when a argument have more than one %f,
	 * but I don't care because this seldom happens and when it does not
	 * much memory will be wasted */
	new_argument = nb_malloc (strlen (argument) + strlen (filename) - 1);
	*c = 0;
	strcpy (new_argument, argument);
	strcat (new_argument, filename);
	offset = strlen (new_argument);
	argument = strcat (new_argument, c + 2);
	break;

      case '%':
	c ++;
	offset = c - argument;
	do 
	  *(c - 1) = *c;
	while (*(c ++));
	break;

      default:
	offset = c + 1 - argument;
      }
    }
    *argv = argument;
  }
}
Esempio n. 7
0
static int db_nessdb_instance_init(char *host, int port)
{
	size_t path_len = strlen(DATA_PATH) + strlen(host) + 5;
	char *path = nb_malloc(path_len);
	snprintf(path, path_len, DATA_PATH, host, port);

	printf("nessDB init: path=%s\n", path);

#if defined(HAVE_NESSDB_V2)
	instance.db = db_open(path);
#elif defined(HAVE_NESSDB_V1)
	instance.db = db_open(path, 0UL, 0);
#endif
	if (instance.db == NULL) {
		printf("db_open() failed\n");
		free(path);
		return -1;
	}
	free(path);

	return 0;
}
Esempio n. 8
0
static void parse_commandline (int argc, char *argv[])
{
  int option;
  struct keep_lines *k;
  char **ap;

  do
  {
    option = getopt (argc, argv, "qsfhn:p:k:v");
    switch (option)
    {
    case 'q':
      remove_quote = 1;
      break;
    case 's':
      remove_sig = 1;
      break;
    case 'f':
      filter = 1;
      break;
    case 'h':
      remove_header = 0;
      break;
    case 'n':
      newsfile_name = optarg;
      break;
    case 'p':
      program = optarg;
      break;
    case 'k':
      k = nb_malloc (sizeof (struct keep_lines));
      k->header = optarg;
      k->next = keep;
      keep = k;
      break;
    case 'v':
      printversion = 1;
      return; /* no more checking needed */

    case ':': /* option with missing parameter */
    case '?': /* unknown option */
      usage ();
    case EOF: /* no more options */
      break;

    default:  /* all possible cases should have been stated */
      nb_error (0, _("getopt returned impossible value: %d ('%c')"),
		option, option);
    }
  }
  while (option != EOF);

  if (newsfile_name == NULL || program == NULL)
    usage ();

  new_argv = nb_malloc ((argc - optind + 2) * sizeof (char *));
  new_argv[0] = program;
  argv += optind;
  ap = new_argv + 1;
  while ((*(ap ++) = *(argv ++)) != NULL)
    ;
}
Esempio n. 9
0
BOOL AFXAPI AfxUnlockTempMaps(BOOL bDeleteTemps)
{
	AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
	if (pState->m_nTempMapLock != 0 && --pState->m_nTempMapLock == 0)
	{
		if (bDeleteTemps)
		{
			if (bDeleteTemps != -1)
			{
				// allow COM libraries to be freed
				CWinThread* pThread = AfxGetThread();
				if (pThread != NULL && pThread->m_lpfnOleTermOrFreeLib != NULL)
					(*pThread->m_lpfnOleTermOrFreeLib)(FALSE, FALSE);
			}

			// clean up temp objects
			// pState->m_pmapHGDIOBJ->DeleteTemp();
			// pState->m_pmapHDC->DeleteTemp();
			// pState->m_pmapHMENU->DeleteTemp();
			// pState->m_pmapHWND->DeleteTemp();
			// pState->m_pmapHIMAGELIST->DeleteTemp();
		}

#ifndef _AFX_PORTABLE
		CWinApp* pApp = AfxGetApp();
		_AFX_THREAD_STATE* pThreadState = _afxThreadState.GetDataNA();
		if( pThreadState != NULL )
		{
			// restore safety pool after temp objects destroyed
			if (pApp != NULL &&
				 (pThreadState->m_pSafetyPoolBuffer == NULL ||
				 _msize(pThreadState->m_pSafetyPoolBuffer) < pApp->m_nSafetyPoolSize) &&
				pApp->m_nSafetyPoolSize != 0)
			{
				// attempt to restore the safety pool to its max size
				size_t nOldSize = 0;
				if (pThreadState->m_pSafetyPoolBuffer != NULL)
				{
					nOldSize = _msize(pThreadState->m_pSafetyPoolBuffer);
					nb_free(pThreadState->m_pSafetyPoolBuffer);
				}

				// undo handler trap for the following allocation
				BOOL bEnable = AfxEnableMemoryTracking(FALSE);
				try
				{
					pThreadState->m_pSafetyPoolBuffer = nb_malloc(pApp->m_nSafetyPoolSize);
					if (pThreadState->m_pSafetyPoolBuffer == NULL)
					{
						// at least get the old buffer back
						if (nOldSize != 0)
						{
							//get it back
							pThreadState->m_pSafetyPoolBuffer = nb_malloc(nOldSize);
							ASSERT(pThreadState->m_pSafetyPoolBuffer != NULL);
						}
					}
				}
				catch( CException * )
				{
					AfxEnableMemoryTracking(bEnable);
					throw;
				}
				AfxEnableMemoryTracking(bEnable);
			}
		}
#endif  // !_AFX_PORTABLE
	}

	// return TRUE if temp maps still locked
	return pState->m_nTempMapLock != 0;
}