コード例 #1
0
ファイル: realloc.c プロジェクト: openmach/openmach
/* XX could be made smarter, so it doesn't always copy to a new block.  */
void *realloc(void *buf, vm_size_t new_size)
{
	vm_size_t *op;
	vm_size_t old_size;
	vm_size_t *np;

	if (buf == 0)
		return malloc(new_size);

	op = (vm_size_t*)buf;
	old_size = *--op;

	new_size += sizeof(vm_size_t);
	while (!(np = lmm_alloc(&malloc_lmm, new_size, 0)))
	{
		if (!morecore(new_size))
			return 0;
	}

	memcpy(np, op, old_size < new_size ? old_size : new_size);

	lmm_free(&malloc_lmm, op, old_size);

	*np++ = new_size;
	return np;
}
コード例 #2
0
ファイル: malloc.c プロジェクト: dmatlack/moridin
void *_malloc(size_t size)
{
	size_t *chunk;

	size += sizeof(size_t);

	if (!(chunk = lmm_alloc(&malloc_lmm, size, 0)))
		return 0;

	*chunk = size;
	return chunk+1;
}
コード例 #3
0
ファイル: kmalloc.c プロジェクト: dmatlack/moridin
static void *__kmalloc(size_t size)
{
	unsigned long flags;
	void *chunk;

	spin_lock_irq(&kmalloc_lock, &flags);

	chunk = lmm_alloc(&kheap_lmm, size, 0);
	if (!chunk)
		goto out;

	kheap_used += size;
out:
	spin_unlock_irq(&kmalloc_lock, flags);
	return chunk;
}
コード例 #4
0
/*
 * The command-line comes to us as a string and contains booting-options,
 * environment-variable settings, and arguments to main().
 * The format is like this:
 *	progname [<booting-options and foo=bar> --] <args to main>
 * For example
 *	kernel DISPLAY=host:0 -d -- -rf foo bar
 * which would be converted into
 *	environ = {"DISPLAY=host:0", 0}
 *	oskit_bootargv = {"-d", 0}
 *	argv = {"kernel", "-rf", "foo", "bar", 0}
 * Actually, those would be pointers into
 *	{"kernel", "-rf", "foo", "bar", 0, "DISPLAY=host:0", 0, "-d", 0}
 * If there is no "--" in the command line, then the entire thing is parsed
 * into argv, no environment vars or booting options are set.
 */
void
base_multiboot_init_cmdline(int *argcp, char ***argvp)
{
	int argc;
	char **argv;

	if (boot_info.flags & MULTIBOOT_CMDLINE) {
		char *cl = (char*)phystokv(boot_info.cmdline);
		unsigned cllen = strlen(cl);
		char *toks[1 + cllen];
		unsigned ntoks, nargs, nvars, nbops;
		char *tok;
		unsigned i, dashdashi;
		char **argbuf;
		unsigned envc;

		/*
		 * Parse out the tokens in the command line.
		 * XXX Might be good to handle quotes.
		 */
		ntoks = 0;
		for (tok = strtok(cl, delim); tok; tok = strtok(0, delim))
			toks[ntoks++] = tok;

		/* After this we assume at least one arg (progname). */
		if (ntoks == 0)
			goto nocmdline;

		/* Look for a "--" and record its index. */
		dashdashi = 0;
		for (i = 0; i < ntoks; i++)
			if (strcmp(toks[i], "--") == 0) {
				dashdashi = i;
				break;
			}

		/* Count number of args, env vars, and bootopts. */
		nargs = 1;		/* for progname */
		nvars = 0;
		nbops = 0;
		for (i = 1; i < dashdashi; i++)
			if (strchr(toks[i], '='))
				nvars++;
			else
				nbops++;
		for (i = dashdashi + 1; i < ntoks; i++)
			nargs++;

		/*
		 * Now we know how big our argbuf is.
		 * argv, environ, and oskit_bootargv will point into this.
		 */
		argbuf = lmm_alloc(&malloc_lmm,
				   sizeof(char *) * (nargs + 1) +
				   sizeof(char *) * (nvars + 1) +
				   sizeof(char *) * (nbops + 1),
				   0);
		assert(argbuf);

		/*
		 * Set up the pointers into argbuf, then fill them in.
		 */
		argv = argbuf;
		environ = argv + nargs + 1;
		oskit_bootargv = environ + nvars + 1;

		argc = 0;
		argv[argc++] = toks[0];
		envc = 0;
		oskit_bootargc = 0;
		for (i = 1; i < dashdashi; i++)
			if (strchr(toks[i], '='))
				environ[envc++] = toks[i];
			else
				oskit_bootargv[oskit_bootargc++] = toks[i];
		for (i = dashdashi + 1; i < ntoks; i++)
			argv[argc++] = toks[i];

		argv[argc] = 0;
		environ[envc] = 0;
		oskit_bootargv[oskit_bootargc] = 0;
	}
	else
	{
nocmdline:
		/* No command line.  */
		argc = 1;
		argv = null_args;
		environ = null_args + 1;
		oskit_bootargc = 0;
		oskit_bootargv = environ;
	}

	*argcp = argc;
	*argvp = argv;
}