Пример #1
0
int main(int argc, char **argv) {
  int myints[] = {10, 20, 30, 5, 15};
  Vector *v = NewVector(int, 5);
  for (int i = 0; i < 5; i++) {
    Vector_Push(v, myints[i]);
  }

  Make_Heap(v, 0, v->top, cmp);

  int n;
  Vector_Get(v, 0, &n);
  assert(30 == n);

  Heap_Pop(v, 0, v->top, cmp);
  v->top = 4;
  Vector_Get(v, 0, &n);
  assert(20 == n);

  Vector_Push(v, 99);
  Heap_Push(v, 0, v->top, cmp);
  Vector_Get(v, 0, &n);
  assert(99 == n);

  Vector_Free(v);
  printf("PASS!\n");
  return 0;
}
Пример #2
0
void Elk_Init (int ac, char **av, int init_objects, char *toplevel) {

/* To avoid that the stack copying code overwrites argv if a dumped
 * copy of the interpreter is invoked with more arguments than the
 * original a.out, move the stack base INITIAL_STK_OFFSET bytes down.
 * The call to memset() is there to prevent the optimizer from removing
 * the array.
 */
#ifdef CAN_DUMP
    char unused[INITIAL_STK_OFFSET];
#endif
    char *initfile, *loadfile = 0, *loadpath = 0;
    int debug = 0, heap = HEAP_SIZE;
    Object file;
    struct stat st;
    extern int errno;
#if defined(CAN_DUMP)
#   define foo (av[0][0])
#else
    volatile char foo;
#endif

#ifdef CAN_DUMP
    memset (unused, 0, 1);  /* see comment above */
#endif
    if (ac == 0) {
        av[0] = "Elk"; ac = 1;
    }
    Get_Stack_Limit ();

    Lib_Dir = NULL;
    Scm_Dir = NULL;

#ifdef WIN32
    if (av[0]) {
        char path[MAX_PATH], *exe;
        GetFullPathName (av[0], MAX_PATH, path, &exe);
        if (exe > path && exe[-1] == '\\') {
            char newpath[MAX_PATH+5];
            exe[-1] = '\0';
            sprintf (newpath, "%s\\lib", path);
            Lib_Dir = strdup (newpath);
            sprintf (newpath, "%s\\scm", path);
            Scm_Dir = strdup (newpath);
        }
    }
#elif defined(FIND_AOUT)
    A_Out_Name = Find_Executable (av[0]);
#endif
    if (Scm_Dir == NULL)
        Scm_Dir = strdup (SCM_DIR);
    if (Lib_Dir == NULL)
        Lib_Dir = strdup (LIB_DIR);

    Argc = ac; Argv = av;
    First_Arg = 1;
#ifdef CAN_DUMP
    if (Was_Dumped) {
        /* Check if beginning of stack has moved by a large amount.
         * This is the case, for instance, on a Sun-4m when the
         * interpreter was dumped on a Sun-4c and vice versa.
         */
        if (abs (stkbase - &foo) > INITIAL_STK_OFFSET) {
            fprintf (stderr,
"Can't restart dumped interpreter from a different machine architecture\n");
            fprintf (stderr,
"   (Stack delta = %lld bytes).\n", (long long int)(intptr_t)(stkbase - &foo));
            exit (1);
        }
        /* Check if program break must be reset.
        */
        if ((intptr_t)Brk_On_Dump && (intptr_t)brk (Brk_On_Dump)
                == (intptr_t)-1) {
            perror ("brk"); exit (1);
        }
#if defined(HP9K) && defined(CAN_DUMP) && defined(HPSHLIB)
        Restore_Shared_Data ();
#endif
#ifdef GENERATIONAL_GC
        Generational_GC_Reinitialize ();
#endif
        Loader_Input = 0;
        Install_Intr_Handler ();
        (void)Funcall_Control_Point (Dump_Control_Point, Arg_True, 0);
        /*NOTREACHED*/
    }
#endif

    for ( ; First_Arg < ac; First_Arg++) {
        if (strcmp (av[First_Arg], "-debug") == 0) {
            debug = 1;
        } else if (strcmp (av[First_Arg], "-g") == 0) {
            Case_Insensitive = 0;
        } else if (strcmp (av[First_Arg], "-i") == 0) {
            Case_Insensitive = 1;
        } else if (strcmp (av[First_Arg], "-v") == 0) {
            if (++First_Arg == ac)
                Usage ();
            if (strcmp (av[First_Arg], "load") == 0)
                Verb_Load = 1;
            else if (strcmp (av[First_Arg], "init") == 0)
                Verb_Init = 1;
            else Usage ();
        } else if (strcmp (av[First_Arg], "-h") == 0) {
            if (++First_Arg == ac)
                Usage ();
            if ((heap = atoi (av[First_Arg])) <= 0) {
                fprintf (stderr, "Heap size must be a positive number.\n");
                exit (1);
            }
        } else if (strcmp (av[First_Arg], "-l") == 0) {
            if (++First_Arg == ac || loadfile)
                Usage ();
            loadfile = av[First_Arg];
        } else if (strcmp (av[First_Arg], "-p") == 0) {
            if (++First_Arg == ac || loadpath)
                Usage ();
            loadpath = av[First_Arg];
        } else if (strcmp (av[First_Arg], "--") == 0) {
            First_Arg++;
            break;
        } else if (av[First_Arg][0] == '-') {
            Usage ();
        } else {
            break;
        }
    }

    stkbase = &foo;
    Stack_Grows_Down = Check_Stack_Grows_Down ();
    ELK_ALIGN(stkbase);
    Make_Heap (heap);
    Init_Everything ();
#ifdef HAVE_ATEXIT
    if (atexit (Exit_Handler) != 0)
        Fatal_Error ("atexit returned non-zero value");
#endif
#ifdef INIT_OBJECTS
    if (init_objects) {
        Set_Error_Tag ("init-objects");
        The_Symbols = Open_File_And_Snarf_Symbols (A_Out_Name);
        Call_Initializers (The_Symbols, (char *)0, PR_EXTENSION);
    }
#endif
    if (loadpath || (loadpath = getenv (LOADPATH_ENV)))
        Init_Loadpath (loadpath);

    /* The following code is sort of a hack.  initscheme.scm should not
     * be resolved against load-path.  However, the .scm-files may not
     * have been installed yet (note that the interpreter is already
     * used in the "make" process).
     * Solution: if initscheme.scm hasn't been installed yet, do search
     * the load-path, so that -p can be used.
     */
    Set_Error_Tag ("scheme-init");
    initfile = Safe_Malloc (strlen (Scm_Dir) + 1 + sizeof (INITFILE) + 1);
    sprintf (initfile, "%s" SEPARATOR_STRING "%s", Scm_Dir, INITFILE);
    if (stat (initfile, &st) == -1 && errno == ENOENT)
        file = Make_String (INITFILE, sizeof(INITFILE)-1);
    else
        file = Make_String (initfile, strlen (initfile));
    free (initfile);
    (void)General_Load (file, The_Environment);

    Install_Intr_Handler ();

    Set_Error_Tag ("top-level");
    if (toplevel == 0) {
        Interpreter_Initialized = 1;
        GC_Debug = debug;
        return;
    }
    /* Special case: if toplevel is "", act as if run from main() */
    if (loadfile == 0 && toplevel[0] != '\0')
        loadfile = toplevel;
    if (loadfile == 0)
        loadfile = "toplevel.scm";
    file = Make_String (loadfile, strlen (loadfile));
    Interpreter_Initialized = 1;
    GC_Debug = debug;
    if (loadfile[0] == '-' && loadfile[1] == '\0')
        Load_Source_Port (Standard_Input_Port);
    else
        (void)General_Load (file, The_Environment);
}