コード例 #1
0
ファイル: allocb.c プロジェクト: 99years/plan9
/*
 * convert malloced or non-malloced buffer to a Block.
 * used to build custom Block allocators.
 *
 * buf must be at least blocksize(usable) bytes.
 */
Block *
mem2block(void *buf, ulong usable, int malloced)
{
	Block *b;

	if(buf == nil)
		return nil;

	b = (Block *)buf;
	b->next = nil;
	b->list = nil;
	b->free = 0;
	b->flag = 0;
	b->ref = 0;
	b->magic = Bmagic;
	_xinc(&b->ref);

	/* align start of data portion by rounding up */
	b->base = (uchar*)ALIGNUP((ulong)b + sizeof(Block));

	/* align end of data portion by rounding down */
	b->lim = (uchar*)b + (malloced? msize(b): blocksize(usable));
	b->lim = (uchar*)((ulong)b->lim & ~(BLOCKALIGN-1));

	/* leave sluff at beginning for added headers */
	b->wp = b->rp = b->lim - ALIGNUP(usable);
	if(b->rp < b->base)
		panic("mem2block: b->rp < b->base");
	if(b->lim > (uchar*)b + (malloced? msize(b): blocksize(usable)))
		panic("mem2block: b->lim beyond Block end");
	return b;
}
コード例 #2
0
ファイル: devprog.c プロジェクト: 8l/inferno
static int
progsize(Prog *p)
{
	int size;
	Frame *f;
	uchar *fp;
	Modlink *m;

	m = p->R.M;
	size = 0;
	if(m->MP != H)
		size += hmsize(D2H(m->MP));
	if(m->prog != nil)
		size += msize(m->prog);

	fp = p->R.FP;
	while(fp != nil) {
		f = (Frame*)fp;
		fp = f->fp;
		if(f->mr != nil) {
			if(f->mr->MP != H)
				size += hmsize(D2H(f->mr->MP));
			if(f->mr->prog != nil)
				size += msize(f->mr->prog);
		}
		if(f->t == nil)
			size += msize(SEXTYPE(f));
	}
	return size/1024;
}
コード例 #3
0
ファイル: main.c プロジェクト: UIKit0/flux
char *svga_rcall(uint64_t source, struct vfs_obj *file, const char *args) {
	char *rets = NULL;
	int x, y, d, w, h;
	int mode;

	if (!strcmp(args, "getmode")) {
		rets = malloc(16);
		sprintf(rets, "%d %d %d", svga.w, svga.h, svga.d);
		return rets;
	}
	if (!strcmp(args, "listmodes")) {
		return strdup(modesstr);
	}
	if (!strcmp(args, "unshare")) {
		mutex_spin(&file->mutex);
		page_free(buffer, msize(buffer));
		free(buffer);
		buffer = valloc(svga.w * svga.h * 4);
		mutex_free(&file->mutex);
		return strdup("T");
	}

	if (!strncmp(args, "setmode ", 8)) {
		if (sscanf(args + 8, "%i %i %i", &x, &y, &d) != 3) {
			return strdup("");
		}
		mutex_spin(&file->mutex);
		mode = svga_find_mode(x, y, d);
		if (svga_set_mode(mode)) {
			return strdup("");
		}
		page_free(buffer, msize(buffer));
		free(buffer);
		buffer = valloc(svga.w * svga.h * 4);
		mutex_free(&file->mutex);
		return strdup("T");
	}

	if (!strncmp(args, "syncrect ", 9)) {
		if (sscanf(args + 9, "%i %i %i %i", &x, &y, &w, &h) != 4) {
			return strdup("");
		}
		mutex_spin(&file->mutex);
		svga_fliprect(buffer, x, y, w, h);
		mutex_free(&file->mutex);
		return strdup("T");
	}
	
	return NULL;
}
コード例 #4
0
ファイル: htonmtest.c プロジェクト: 8l/csolve
main(void)
{
	MINT io;
	int b, blk;
	int cplen;
	char *cp;

	while (1) {
		MINIT(&io);
		printf("base: ");
		scanf("%d",&b);
		printf("blanks: ");
		scanf("%d",&blk);
		m_in_b(&io,b,stdin,blk);
		printf("\nOutbase: ");
		scanf("%d",&b);
		printf("Outblanks: ");
		scanf("%d",&blk);
		printf("\nOut: "); m_out_b(&io,b,stdout,blk);
		putchar('\n');
		cp = htonm(&io);
		cplen = msize(&io);
		MFREE(&io);
		printf("cplen = %d\n",cplen);
		MINIT(&io);
		ntohm(&io,cp);
		free(cp);
		printf("To-Network-To-Host: "); m_out_b(&io,b,stdout,blk);
		putchar('\n');
		if (feof(stdin)) break;
	}
}
コード例 #5
0
ファイル: main.c プロジェクト: jrepan/rhombus
char *svga_rcall_unshare(struct robject *self, rp_t source, int argc, char **argv) {

	mutex_spin(&self->driver_mutex);
	page_free(buffer, msize(buffer));
	free(buffer);
	buffer = valloc(svga.w * svga.h * 4);
	mutex_free(&self->driver_mutex);

	return strdup("T");
}
コード例 #6
0
ファイル: EditBoxLayer.cpp プロジェクト: johndpope/Medusa
bool EditBoxLayer::Initialize()
{
	RETURN_FALSE_IF_FALSE(BaseCaseLayer::Initialize());


	INode* sprite=NodeFactory::Instance().CreateSingleLineEditBox(msize(500,180), FontId("PaperBlack.ttf",96),L"ABC");
	sprite->SetDock(DockPoint::MiddleCenter);
	sprite->SetAnchor(0.5f, 0.5f);
	
	AddChild(sprite);

	return true;
}
コード例 #7
0
ファイル: BumpAllocator.cpp プロジェクト: Waqee/sirikata
    void free(void *ptr) {
        if (ptr == NULL) {
            return;
        }
        
        size_t amount_requested = msize(ptr);
        size_t amount_with_header = amount_requested + sizeof(size_t);
        size_t new_offset = (size_t)((uint8*)ptr - data_start) - sizeof(size_t);
#ifdef USE_BUILTIN_ATOMICS_FOR_ALLOCATOR
        size_t original_check = new_offset + amount_with_header;
        allocated.compare_exchange_weak(original_check, new_offset);
#endif
    }
コード例 #8
0
ファイル: opentest.c プロジェクト: linuxfs/tools
int main(void)
{
	int fd, i, operr, mode[4];
	char buf[16], *prin[4];

	mode[0] = O_RDWR | O_CREAT;
	mode[1] = O_RDWR | O_CREAT | O_TRUNC;
	mode[2] = O_RDWR | O_CREAT | O_TRUNC | O_EXCL;
	mode[3] = O_RDWR | O_CREAT |           O_EXCL;
	prin[0] = "O_CREAT                   ";
	prin[1] = "O_CREAT | O_TRUNC         ";
	prin[2] = "O_CREAT | O_TRUNC | O_EXCL";
	prin[3] = "O_CREAT |           O_EXCL";
	errno = 0;

	/* test start */
	for (i=0;i<4;i++) {
		unlink(FILES);
		fd = open(FILES, mode[i], 0644);
		operr = errno;
		errno = 0;
		if (fd > 0) {
			write(fd, &buf, 16);
			close(fd);
		}
		printf("open ENOENT %s %2d %2d %2ld\n", prin[i], fd, operr, msize());
		fd = open(FILES, mode[i], 0644);
		operr = errno;
		errno = 0;
		if (!(fd < 0)) {
			close(fd);
		}
		printf("open EEXIST %s %2d %2d %2ld\n", prin[i], fd, operr, msize());
	}
	return 0;
}
コード例 #9
0
ファイル: alloc.c プロジェクト: mrshu/school
/**
 * Inicializacia pamate
 *
 * Zavola sa, v stave, ked sa zacina s prazdnou pamatou, ktora je inicializovana
 * na 0.
 */
void my_init(void) {
	//split 32-bit int 4 bytes 
	unsigned int total_size = msize();
	uint8_t size_part_4 = total_size % (1<<8);
	total_size = total_size>>8;
	uint8_t size_part_3 = total_size % (1<<8);
	total_size = total_size>>8;
	uint8_t size_part_2 = total_size % (1<<8);
	total_size = total_size>>8;
	uint8_t size_part_1 = total_size % (1<<8);
	// 0..3 bytes -> total memory
	mwrite(0,size_part_1);
	mwrite(1,size_part_2);
	mwrite(2,size_part_3);
	mwrite(3,size_part_4);

	return;
}
コード例 #10
0
ファイル: main.c プロジェクト: jrepan/rhombus
int svga_share(struct robject *self, rp_t source, uint8_t *_buffer, size_t size, uint64_t off) {

	if (size != svga.w * svga.h * 4) {
		return -1;
	}
	if (off != 0) {
		return -1;
	}

	if (buffer) {
		page_free(buffer, msize(buffer));
		free(buffer);
	}

	buffer = (uint32_t*) _buffer;

	return 0;
}
コード例 #11
0
ファイル: exec.c プロジェクト: jrepan/rhombus
int execiv(uint8_t *image, size_t size, char const **argv) {
	struct dl_list *list;
	char *argv_pack;

	if (!image) {
		errno = ENOENT;
		return -1;
	}

	/* build list for linker */
	list = malloc(sizeof(struct dl_list));
	list[0].type = DL_EXEC;
	list[0].base = image;
	list[0].size = size;
	list[0].name[0] = '\0';

	/* save standard streams and filesystem root */
	fdsave(0, fd_rp(0));
	fdsave(1, fd_rp(1));
	fdsave(2, fd_rp(2));
	fdsave(3, fs_root);

	/* save argument list */
	if (argv) {
		argv_pack = packarg(argv);
		__pack_add(PACK_KEY_ARG, argv_pack, msize(argv_pack));
		free(argv_pack);
	}

	/* save environment variables */
	__saveenv();

	/* persist saved stuff */
	__pack_save();

	if (dl_exec(list, 1)) {
		errno = ENOEXEC;
		return -1;
	}

	return 0;
}
コード例 #12
0
ファイル: main.c プロジェクト: jrepan/rhombus
char *svga_rcall_setmode(struct robject *self, rp_t source, int argc, char **argv) {
	int x, y, d;
	int mode;

	if (argc != 4) return NULL;

	x = atoi(argv[1]);
	y = atoi(argv[2]);
	d = atoi(argv[3]);

	mutex_spin(&self->driver_mutex);
	mode = svga_find_mode(x, y, d);
	if (svga_set_mode(mode)) return NULL;
	page_free(buffer, msize(buffer));
	free(buffer);
	buffer = valloc(svga.w * svga.h * 4);
	mutex_free(&self->driver_mutex);

	return strdup("T");
}
コード例 #13
0
ファイル: main.c プロジェクト: UIKit0/flux
int svga_share(uint64_t source, struct vfs_obj *file, uint8_t *_buffer, size_t size, uint64_t off) {

	if (size != svga.w * svga.h * 4) {
		return -1;
	}
	if (off != 0) {
		return -1;
	}

	mutex_spin(&file->mutex);

	if (buffer) {
		page_free(buffer, msize(buffer));
		free(buffer);
	}

	buffer = (uint32_t*) _buffer;

	mutex_free(&file->mutex);

	return 0;
}
コード例 #14
0
ファイル: devlogfs.c プロジェクト: 8l/inferno
void *
logfsrealloc(void *p, ulong size)
#endif
{
	void *q;
	ulong osize;
	if (waserror()) {
		print("wobbly thrown in memory allocator: %s\n", up->env->errstr);
		nexterror();
	}
	if (p == nil) {
		q = smalloc(size);
		poperror();
#ifdef LEAKHUNT
		leakrealloc(q, nil, callerpc);
#endif
		return q;
	}
	q = realloc(p, size);
	if (q) {
		poperror();
#ifdef LEAKHUNT
		leakrealloc(q, p, callerpc);
#endif
		return q;
	}
	q = smalloc(size);
	osize = msize(p);
	if (osize > size)
		osize = size;
	memmove(q, p, osize);
	free(p);
	poperror();
#ifdef LEAKHUNT
	leakrealloc(q, p, callerpc);
#endif
	return q;
}
コード例 #15
0
ファイル: BumpAllocator.cpp プロジェクト: Waqee/sirikata
    void *realloc(void * ptr, size_t amount, size_t *ret_size, bool movable) {
        assert(amount > 0 && "Free should have been called if 0 size");
        size_t ptr_actual_size = msize(ptr);
        if (ptr_actual_size >= amount) {
            *ret_size = ptr_actual_size;
            return ptr;
        }
        if (ptr && !movable) {
            return NULL;
        }
        size_t amount_requested = amount + sizeof(size_t); // amount + size of amount
        if ((amount_requested & (alignment - 1)) != 0) {
            amount_requested += alignment - (amount_requested & (alignment - 1));
        }        
        if (allocated.load() + amount_requested > max) {
            return NULL;
        }

        size_t offset = (allocated += amount_requested);
        if (offset > max) {
            //allocated -= amount; // FIXME <-- can this help?
            return NULL;
        }
        size_t amount_returned = amount_requested - sizeof(size_t);
        if (ret_size) {
            *ret_size = amount_returned;
        }
        memcpy(data_start + offset - amount_requested, &amount_returned, sizeof(size_t));
        uint8* retval = data_start + offset - amount_returned;
        assert(((size_t)retval & (alignment - 1)) == 0);
        if (ptr) {
            memcpy(retval, ptr, std::min(amount, ptr_actual_size));
            this->free(ptr);
        }
        return retval;
    }
コード例 #16
0
ファイル: exec.c プロジェクト: jrepan/rhombus
int execv(const char *path, char const **argv) {
	void *image;

	image = load_exec(path);
	return execiv(image, msize(image), argv);
}