Exemplo n.º 1
0
void skit_text_stream_append(skit_text_stream *stream, skit_slice slice)
{
	sASSERT(stream != NULL);
	sASSERT(sSPTR(slice) != NULL);
	skit_text_stream_internal *tstreami = &(stream->as_internal);
	
	skit_tstream_internal_append(tstreami, slice);
	
	tstreami->cursor = sSLENGTH(tstreami->text) + 1;
}
Exemplo n.º 2
0
static void skit_math_macro_test()
{
	int foo = 5;
	double bar;
	bar = SKIT_MAX(foo, 6, foo+3.5, 1.2);
	sASSERT(bar == 8.5);
	bar = SKIT_MIN(foo, 6, foo+3.5, 1.2);
	sASSERT(bar == 1.2);
	printf("  skit_math_macro_test passed.\n");
}
Exemplo n.º 3
0
void skit_text_stream_appendln(skit_text_stream *stream, skit_slice line)
{
	sASSERT(stream != NULL);
	sASSERT(sSPTR(line) != NULL);
	skit_text_stream_internal *tstreami = &(stream->as_internal);
	
	skit_tstream_internal_append(tstreami, line);
	skit_tstream_internal_append(tstreami, sSLICE("\n"));
	
	tstreami->cursor += sSLENGTH(tstreami->text) + 1;
}
Exemplo n.º 4
0
void skit_pfile_stream_close(skit_pfile_stream *stream)
{
	SKIT_USE_FEATURE_EMULATION;
	sASSERT(stream != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	if( pstreami->file_handle == NULL )
		sTRACE(skit_stream_throw_exc(SKIT_FILE_IO_EXCEPTION, &(stream->as_stream), "Attempt to close an unopened stream."));

	sASSERT(pstreami->closer_func != NULL);
	sTRACE1(pstreami->closer_func(stream, pstreami->closer_func_arg, pstreami->file_handle));

	pstreami->file_handle = NULL;
}
Exemplo n.º 5
0
void skit_pfile_stream_appendf_va(skit_pfile_stream *stream, const char *fmtstr, va_list vl )
{
	SKIT_USE_FEATURE_EMULATION;
	sASSERT(stream != NULL);
	sASSERT(fmtstr != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	if( pstreami->file_handle == NULL )
		sTRACE(skit_stream_throw_exc(SKIT_FILE_IO_EXCEPTION, &(stream->as_stream), "Attempt to write to an unopened stream."));
	
	vfprintf( pstreami->file_handle, fmtstr, vl ); /* TODO: error handling? */
	if ( skit_pfile_stream_check_flush(pstreami, skit_slice_of_cstr(fmtstr)) )
		skit_pfile_stream_flush(stream);
}
Exemplo n.º 6
0
void skit_text_stream_appendf_va(skit_text_stream *stream, const char *fmtstr, va_list vl)
{
	const size_t buf_size = 1024;
	char buffer[buf_size];
	size_t nchars_printed = 0;
	sASSERT(stream != NULL);
	sASSERT(fmtstr != NULL);
	
	skit_text_stream_internal *tstreami = &(stream->as_internal);
	
	nchars_printed = vsnprintf(buffer, buf_size, fmtstr, vl);
	skit_tstream_internal_append(tstreami, skit_slice_of_cstrn(buffer, nchars_printed));
	
	tstreami->cursor = sSLENGTH(tstreami->text) + 1;
}
Exemplo n.º 7
0
skit_slice skit_text_stream_slurp(skit_text_stream *stream, skit_loaf *buffer)
{
	sASSERT(stream != NULL);
	skit_text_stream_internal *tstreami = &(stream->as_internal);
	tstreami->cursor = sSLENGTH(tstreami->text);
	return tstreami->text;
}
Exemplo n.º 8
0
skit_slice skit_text_stream_read(skit_text_stream *stream, skit_loaf *buffer, size_t nbytes)
{
	sASSERT(stream != NULL);
	
	skit_text_stream_internal *tstreami = &(stream->as_internal);
	size_t cursor = tstreami->cursor; /* Create a fast, local copy of the cursor. */
	skit_slice text = tstreami->text; /* Ditto for the text slice. */
	ssize_t length = sSLENGTH(text);  /* Ditto for length.  It won't be changing. */
	
	/* Return null when reading past the end of the stream. */
	if ( cursor >= length )
		return skit_slice_null();
	
	size_t block_begin = cursor;
	size_t block_end = block_begin + nbytes;
	
	if ( length < block_end )
	{
		/* Saturate at the end bounds. */
		block_end = length;
		
		/* We've read /past/ the end of the stream.  Demonstrate this by */
		/*   tossing the cursor over the end. */
		tstreami->cursor = block_end+1;
	}
	else
	{
		/* Normal case: update the cursor. */
		tstreami->cursor = block_end;
	}
	
	return skit_slice_of( text, block_begin, block_end );
}
Exemplo n.º 9
0
sThreadLock::~sThreadLock()
{
  sASSERT(lock);
  if(pthread_mutex_destroy((pthread_mutex_t *)lock))
    sFatal("pthread_mutex_destroy failed");
  delete (pthread_mutex_t *) lock;
}
Exemplo n.º 10
0
static void skit_peg_bd_test_callback( skit_peg_parser *parser, ssize_t cursor_reset_pos )
{
	int *which_discard = (int*)parser->caller_context;
	
	switch( *which_discard )
	{
		// branch_discard_1
		case 0: sASSERT_EQ( cursor_reset_pos, 1 ); break;
		case 1: sASSERT_EQ( cursor_reset_pos, 1 ); break;
		case 2: sASSERT_EQ( cursor_reset_pos, 1 ); break;
		case 3: sASSERT_EQ( cursor_reset_pos, 2 ); break;
		case 4: sASSERT_EQ( cursor_reset_pos, 3 ); break;
		
		// branch_discard_2
		case 5: sASSERT_EQ( cursor_reset_pos, 4 ); break;
		case 6: sASSERT_EQ( cursor_reset_pos, 9 ); break;
		
		// branch_discard_3
		case 7: sASSERT_EQ( cursor_reset_pos, 1 ); break;
		case 8: sASSERT_EQ( cursor_reset_pos, 0 ); break;
		
		// There should be no other discards.
		default: sASSERT(0);
	}
	
	(*which_discard)++;
}
Exemplo n.º 11
0
skit_slice skit_pfile_stream_read(skit_pfile_stream *stream, skit_loaf *buffer, size_t nbytes)
{
	SKIT_USE_FEATURE_EMULATION;
	sASSERT(stream != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	skit_loaf *read_buf;
	
	/* Return NULL slices when attempting to read from an already-exhausted stream. */
	if ( feof(pstreami->file_handle) )
		return skit_slice_null();
	
	/* Figure out which buffer to use. */
	read_buf = skit_pfile_get_read_buffer(pstreami, buffer);
	
	/* The provided buffer might not be large enough. */
	/* Thankfully, the necessary size is easy to calculate in this case. */
	if ( sLLENGTH(*read_buf) < nbytes )
		skit_loaf_resize(read_buf, nbytes);
	
	/* Do the read. */
	size_t nbytes_read = fread( sLPTR(*read_buf), 1, nbytes, pstreami->file_handle );
	if ( ferror(pstreami->file_handle) )
	{
		char errbuf[1024];
		sTRACE(skit_stream_throw_exc(SKIT_FILE_IO_EXCEPTION, &(stream->as_stream), skit_errno_to_cstr(errbuf, sizeof(errbuf))));
	}
	
	return skit_slice_of(read_buf->as_slice, 0, nbytes_read);
}
Exemplo n.º 12
0
void skit_pfile_stream_flush(skit_pfile_stream *stream)
{
	SKIT_USE_FEATURE_EMULATION;
	sASSERT(stream != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	if( pstreami->file_handle == NULL )
		sTRACE(skit_stream_throw_exc(SKIT_FILE_IO_EXCEPTION, &(stream->as_stream), "Attempt to flush an unopened stream."));
	
	#ifdef __VMS
	/* 
	On OpenVMS, fflush does not force writes onto disk, but fsync does. 
	Thus, the behavior the caller will probably expect is that of fsync.
	Unittests will fail on OpenVMS if fflush is used.  Using fflush makes
	it impossible to have one pfile_stream read the writes created by
	another concurrent pfile_stream open to the same file.
	Source: http://h71000.www7.hp.com/commercial/c/docs/5763p029.html
	*/
	int errval = fsync(fileno(pstreami->file_handle));
	#else
	/*
	Linux, on the other hand, seems to prefer fflush as a way to make
	writes immediately available to other open streams.  This was found
	by expermintation after the VMS path made unittests fail.
	*/
	int errval = fflush(pstreami->file_handle);
	#endif
	
	if ( errval != 0 )
	{
		char errbuf[1024];
		/* TODO: throw exception for invalid streams when (errval == EBADF) */
		sTRACE(skit_stream_throw_exc(SKIT_FILE_IO_EXCEPTION, &(stream->as_stream), skit_errno_to_cstr(errbuf, sizeof(errbuf))));
	}
}
Exemplo n.º 13
0
void sQueryQueue::End()
{
    sASSERT(Current!=0);

    Current->End(Adapter->ImmediateContext);
    PendingQueries.AddTail(Current);
    Current = 0;
}
Exemplo n.º 14
0
skit_pfile_stream *skit_pfile_stream_downcast(const skit_stream *stream)
{
	sASSERT(stream != NULL);
	if ( stream->meta.vtable_ptr == &skit_pfile_stream_vtable )
		return (skit_pfile_stream*)stream;
	else
		return NULL;
}
Exemplo n.º 15
0
void skit_pfile_stream_append(skit_pfile_stream *stream, skit_slice slice)
{
	sASSERT(stream != NULL);

	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	skit_pfile_stream_append_int(stream, slice);
	if ( skit_pfile_stream_check_flush(pstreami, slice) )
		skit_pfile_stream_flush(stream);
}
Exemplo n.º 16
0
bool sExecuteShellDetached(const char *cmdline,const char *dir)
{
  sASSERT(dir==0);
  sLogF("file","Execute detached <%s>",cmdline);
  if(fork()==0)
  {
    int ignore = system(cmdline);
    _exit(127+ignore*0);
  }
  return 1;
}
Exemplo n.º 17
0
bool sExecuteShell(const char *cmdline,const char *dir)
{
  sASSERT(dir==0);
  sLogF("file","Execute <%s>",cmdline);
  if(system(cmdline)!=0)
  {
    sLogF("file","Execute failed");
    return 0;
  }
  return 1;
}
Exemplo n.º 18
0
void skit_pfile_stream_flush_cond(
	skit_pfile_stream *stream,
	void *arg,
	int (*condition)( void *arg, skit_slice text_written )
)
{
	sASSERT(stream != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	pstreami->flush_condition = condition;
	pstreami->flush_condition_arg = arg;
}
Exemplo n.º 19
0
void Init()
{
    sASSERT(Active==0);
    Active = 2;
    MemLeakLock = new sThreadLock();
    Free = 0;
    LeakCount = 0;
    sClear(Hash);
    FirstBlock = 0;
    atexit(Exit);
    Active = 1;
    sLog("sys","leak tracking activated");
}
Exemplo n.º 20
0
sPerfMonThread::sPerfMonThread(const char *name,int history) : sPerfMonBase(name)
{
    sASSERT(history>=2);
    History = history;
    for(int i=0; i<history; i++)
        Frames.Add(new Frame);
    for(int i=0; i<history; i++)
        Frames[i]->Records.HintSize(4096);
    DoubleBuffer = 0;
    Current = Frames[0];
    Flip();
    Flip();
    sPerfMonSubsystem_.Add(this);
}
Exemplo n.º 21
0
skit_slice skit_pfile_stream_readln(skit_pfile_stream *stream, skit_loaf *buffer)
{
	sASSERT(stream != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	skit_loaf *read_buf;
	size_t buf_len;
	
	/* Return NULL slices when attempting to read from an already-exhausted stream. */
	if ( feof(pstreami->file_handle) )
		return skit_slice_null();
	
	/* Figure out which buffer to use. */
	read_buf = skit_pfile_get_read_buffer(pstreami, buffer);
	buf_len = sLLENGTH(*read_buf);
	
	/* Just say NO to improperly sized buffers. */
	if ( buf_len < 2 )
	{
		read_buf = skit_loaf_resize(read_buf, 16);
		buf_len = sLLENGTH(*read_buf);
	}
	
	/* Fill the buffer with one line. */
	skit_slice next_chunk = read_buf->as_slice;
	size_t nbytes_read = 0;
	size_t offset = 0;
	int done = 0;
	while ( 1 )
	{
		skit_try_fgets(pstreami, next_chunk, &nbytes_read, &done);
		if ( done )
			break; /* success! */
		
		/* Upsize the buffer: we need more. */
		buf_len = (buf_len * 3) / 2;
		read_buf = skit_loaf_resize(read_buf, buf_len);
		
		/* Advance the offset in our buffer to point to the new chunk. */
		offset += nbytes_read;
		next_chunk = skit_slice_of(read_buf->as_slice, offset, buf_len);
	}
	
	/* skit_try_fgets only knows about a potion of the entire buffer, and */
	/*   it returns how many bytes were written to that. */
	/* This is how we consider the rest of the buffer: */
	ssize_t line_length = offset + nbytes_read;
	
	/* The rest is trivial. */
	return skit_slice_of(read_buf->as_slice, 0, line_length);
}
Exemplo n.º 22
0
const char *skit__pfile_stream_populate(skit_pfile_stream *stream, skit_slice fname, const char *access_mode)
{
	SKIT_USE_FEATURE_EMULATION;
	sASSERT(stream != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	
	if ( pstreami->file_handle != NULL )
		sTRACE(skit_stream_throw_exc(SKIT_FILE_IO_EXCEPTION, &(stream->as_stream), "Already open to another file."));
	
	pstreami->name = skit_loaf_dup(fname);
	pstreami->access_mode = skit_loaf_copy_cstr(access_mode);
	
	return skit_loaf_as_cstr(pstreami->name);
}
Exemplo n.º 23
0
void skit_pfile_stream_appendln(skit_pfile_stream *stream, skit_slice line)
{
	SKIT_USE_FEATURE_EMULATION;
	sASSERT(stream != NULL);
	/* The error handling is handled by subsequent calls. */
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	int do_flush = 0;
	skit_pfile_stream_append_int(stream, line);
	do_flush |= skit_pfile_stream_check_flush(pstreami, line);
	sTRACE(skit_pfile_stream_newline(pstreami));
	do_flush |= skit_pfile_stream_check_flush(pstreami, sSLICE("\n"));

	if ( do_flush )
		skit_pfile_stream_flush(stream);
}
Exemplo n.º 24
0
skit_slice skit_pfile_stream_slurp(skit_pfile_stream *stream, skit_loaf *buffer)
{
	SKIT_USE_FEATURE_EMULATION;
	
	sASSERT(stream != NULL);
	skit_pfile_stream_internal *pstreami = &(stream->as_internal);
	skit_loaf *read_buf;
	
	/* Figure out which buffer to use. */
	read_buf = skit_pfile_get_read_buffer(pstreami, buffer);
	
	/* Delegate the ugly stuff to the skit_stream_buffered_slurp function. */
	skit_slice result = sETRACE(skit_stream_buffered_slurp(stream, read_buf, &skit_pfile_slurp_source));
	return result;
}
Exemplo n.º 25
0
skit_slice skit_text_stream_readln(skit_text_stream *stream, skit_loaf *buffer)
{
	sASSERT(stream != NULL);
	/* We don't care what the caller passed for buffer.  All of our strings
	are in memory anyways, so we can just pass out slices of those. */
	
	skit_text_stream_internal *tstreami = &(stream->as_internal);
	size_t cursor = tstreami->cursor; /* Create a fast, local copy of the cursor. */
	skit_slice text = tstreami->text; /* Ditto for the text slice. */
	ssize_t length = sSLENGTH(text);  /* Ditto for length.  It won't be changing. */
	
	/* Return null when reading past the end of the stream. */
	if ( cursor > length )
		return skit_slice_null();
	
	/* cursor isn't past the end of stream, so grab the next line. */
	ssize_t line_begin = cursor;
	int nl_size = 0;
	while ( cursor < length )
	{
		nl_size = skit_slice_match_nl(text,cursor);
		if ( nl_size > 0 )
			break;
		cursor++;
	}
	ssize_t line_end = cursor;
	
	
	if ( cursor == length )
	{
		/* This should only happen if we were already positioned at the end of */
		/*   the stream.  Such can happen if the stream had a '\n' character at */
		/*   the very end that causes one last empty line to be read.  If this */
		/*   statement is true, then a previous call already read that empty line */
		/*   and cursor==length means that we are /past/ the end-of-stream now. */
		tstreami->cursor = length+1;
	}
	else
	{
		/* Skip the newline.  We just handled it. */
		cursor += nl_size;
		
		/* Since we used a local variable for cursor, we need to update the original. */
		tstreami->cursor = cursor;
	}
	
	return skit_slice_of( text, line_begin, line_end );
}
Exemplo n.º 26
0
void sQueryQueue::Begin()
{
    sASSERT(Current==0);

    if(FreeQueries.IsEmpty())
    {
        Current = new sGpuQuery(Adapter,Kind);
        AllQueries.Add(Current);
    }
    else
    {
        Current = FreeQueries.RemTail();
    }

    Current->Begin(Adapter->ImmediateContext);
}
Exemplo n.º 27
0
void Exit()
{
    sLog("sys","leak tracking deactivated");
    sASSERT(Active==1);
    sDumpMemory();
    Active=2;
    LeakBlock *b = FirstBlock;
    LeakBlock *n;
    sDelete(MemLeakLock);

    while(b)
    {
        n = b->Next;
        sFreeMemSystem(b);
        b = n;
    }
}
Exemplo n.º 28
0
void sSubsystem::Register(const char *name,int pri)
{
    if(Name==0)
    {
        Name = name;
        Priority = pri;
        Next = First;
        First = this;
        sASSERT(pri>0);

        if(Runlevel>=Priority)
        {
            sLogF("sys","init %02x: %s",pri,name);
            Init();
        }
    }
}
Exemplo n.º 29
0
void skit_pfile_stream_close_with(
	skit_pfile_stream *pstream,
	void *arg,
	void (*closer_func)( skit_pfile_stream *pstream, void *arg, FILE *file_handle )
)
{
	SKIT_USE_FEATURE_EMULATION;
	sASSERT(pstream != NULL);
	skit_pfile_stream_internal *pstreami = &(pstream->as_internal);

	if ( closer_func == NULL )
		pstreami->closer_func = &skit_pfile_stream_non_closer;
	else
		pstreami->closer_func = closer_func;

	pstreami->closer_func_arg = arg;
}
Exemplo n.º 30
0
  sFile *Create(const char *name,sFileAccess access)
  {
    static const char *mode[] =  {  0,"read","readrandom","write","writeappend","readwrite"  };
    static const char *umode[] =  {  0,"rb","rb","wb","ab","rb+"  };
    sASSERT(access>=sFA_Read && access<=sFA_ReadWrite);

    FILE *file = fopen(name,umode[access]);
    
    if(file)
    {
      sLogF("file","open file <%s> for %s",name,mode[access]);
      sFile *f = new sRootFile(file,access);
      return f;
    }
    else 
    {
      sLogF("file","failed to open file <%s> for %s",name,mode[access]);
      return 0;
    }
  }