Ejemplo n.º 1
0
static void
deeper (void (*f) (void))
{
  char stack[1100 * 1024];
  memfrob (stack, sizeof stack);
  (*f) ();
  memfrob (stack, sizeof stack);
}
void
MyFrobulatingFree(void *p)
{
  size_t *stp = (size_t*)p;
  if (p == NULL)
    return;
  memfrob(p, stp[-1]);
  free(stp - 1);
}
Ejemplo n.º 3
0
int main(){
	char *s="01234567890";
	char *p=index(s, '0');
	assertEquals(11, strlen(p));
	p++;
	p=index(p, '0');
	assertEquals(1, strlen(p));
	
	p=rindex(s, '0');
	assertEquals(0, strcmp("0", p));
	
	//memccpy 返回的是目的地串中找到的特定字符的下一个字符指针位置, 并且复制也只发生到特定字符为止,如果目标串中并无发现特定字符,则不发生复制,返回值亦为NULL。
	char a[]="string[a]";
	char b[]="string(b)";
	p = memccpy(a, b, 'b', sizeof(a));
	assert(printf("a:%s\tp:%s\n",a, p)>0);
	assertEquals(0, strcmp("string(b]", a));
	assertEquals(0, strcmp("]", p));
	p = memccpy(a, b, 'c', sizeof(a));
	assertEquals(NULL, p);
	
	char a1[]="hello moto";
	assertEquals(11, sizeof(a1));
	p = (char*)memchr((void *)a1, 'o', sizeof(a1));
	assertEquals(0, strcmp("o moto", p));
	
#if 0
	//memfrob is only supported by linux
	char a2[]="this is a critical section data";
	p = (char*)memfrob( (void*)a2, sizeof(a2));
	printf("First call memfrob: %s length:%d", p, strlen(p));
	
	p = (char*)memfrob( (void*)a2, sizeof(a2));
	printf("Second call memfrob: %s length:%d", p, strlen(p));
#endif
	
	strcspn();
	
	return 0;
}
void* MyRealloc(void* x, size_t size)
{
#ifdef FROBONFREE
   size_t old_size = ((size_t*)x)[-1];
   if (old_size > size)
     memfrob(((char*)x) + size, old_size - size);
   x = realloc(((size_t*)x) - 1, size + sizeof(size_t));
#else
  x = realloc(x, size);
#endif
  if (!x)
    (*noMemHandler)();
  /* Both are needed in all cases to work with realloc... */
#if defined(FROBONMALLOC) && defined(FROBONFREE)
  if (old_size < size)
    memfrob(((char*)x) + old_size, size - old_size);
#endif
#ifdef FROBONFREE
  *(size_t*)x = size;
  x =  ((size_t*)x) + 1;
#endif
  return x;
}
void* MyMalloc(size_t size)
{
  void* p = 
#ifdef FROBONFREE
    malloc(size + sizeof(size_t));
#else
    malloc(size);
#endif
  if (!p)
    (*noMemHandler)();
#ifdef FROBONFREE
  *(size_t*)p = size;
  p =  ((size_t*)p) + 1;
#endif
#ifdef FROBONMALLOC
  memfrob(p, size);
#endif
  return p;
}