예제 #1
0
파일: module.c 프로젝트: E-LLP/n900
void *module_alloc(unsigned long size)
{
	if (size == 0)
		return NULL;

	return vmalloc_exec(size);
}
예제 #2
0
int AMXAPI aux_LoadProgram(AMX *amx, const char *filename)
{
  int error;
  FILE *fp;
  AMX_HEADER hdr;
  void *pcode = NULL;
  void *ncode = NULL;  // ncode = new machine code
  void *rt = NULL;     // rt = relocation table (temporary)

  /* open the file */
  error = AMX_ERR_NOTFOUND;     /* assume "file not found" */
  if ((fp = fopen(filename, "rb")) == NULL)
    goto fail;

  /* read the header */
  error = AMX_ERR_FORMAT;       /* assume "invalid file format" */
  fread(&hdr, sizeof hdr, 1, fp);
  amx_Align16(&hdr.magic);
  if (hdr.magic != AMX_MAGIC)
    goto fail;

  /* allocate memory for the P-code */
  error = AMX_ERR_MEMORY;       /* assume "insufficient memory" */
  amx_Align32((uint32_t *)&hdr.stp);
  amx_Align32((uint32_t *)&hdr.size);
  if ((pcode = malloc((size_t)hdr.stp)) == NULL)
    goto fail;

  /* read the P-code and initialize the abstract machine */
  rewind(fp);
  fread(pcode, 1, (int)hdr.size, fp);
  fclose(fp);
  fp = NULL;
  memset(amx, 0, sizeof *amx);
  amx->flags = AMX_FLAG_JITC;
  if ((error = amx_Init(amx, pcode)) != AMX_ERR_NONE)
    goto fail;

  /* Now we have an initialized abstract machine, which is normally immediately
   * runnable by amx_Exec(). Instead of running the code, we throw it at the
   * JIT compiler...
   */

  /* allocate memory for the compiled instructions and the temporary
   * relocation table
   */
  error = AMX_ERR_MEMORY;       /* assume "insufficient memory" */
  ncode = malloc(amx->code_size);/* amx->code_size is an estimate */
  if (ncode == NULL)
    goto fail;
  if (amx->reloc_size > 0) {
    rt = malloc(amx->reloc_size);
    if (rt == NULL)
      goto fail;
  } /* if */

  /* JIT rulz! (TM) */
  if ((error = amx_InitJIT(amx, rt, ncode)) != AMX_ERR_NONE)
    goto fail;

  /* The compiled code is relocatable, since only relative jumps are
   * used for destinations within the generated code and absoulute
   * addresses for jumps into the runtime, which is fixed in memory.
   */
  error = AMX_ERR_MEMORY;       /* assume "insufficient memory" */
  if ((amx->base = vmalloc_exec(amx->code_size)) == NULL)
    goto fail;
  memcpy(amx->base, ncode, amx->code_size);
  free(pcode);
  free(rt);
  free(ncode);
  return AMX_ERR_NONE;

fail:
  if (fp != NULL)
    fclose(fp);
  if (pcode != NULL)
    free(pcode);
  if (rt != NULL)
    free(rt);
  if (ncode != NULL)
    free(ncode);
  memset(amx, 0, sizeof *amx);
  return error;
}