Ejemplo n.º 1
0
void free_storage(void* storage) {
    switch(*(char*)storage) {
        case 'd': {
            printf("free dict\n");
            Dictionary* dict = (Dictionary*)storage;
            for(int i = 0; i < dict->count; i++) {
                free_storage(dict->values[i]);
                free(dict->keys[i]);
            }
            free(dict->values);
            free(dict->keys);
            free(dict);
            break;
        }
        case 'l': {
            printf("free list\n");
            List* list = (List*)storage;
            for(int i = 0; i < list->count; i++) {
                free_storage(list->values[i]);
            }
            free(list->values);
            free(list);
            break;
        }
        default: {
            printf("free val\n");
            Value* val = (Value*)storage;
            free(val->value);
            free(val);
        }
    }
}
Ejemplo n.º 2
0
static void
generate_states(void)
{
    allocate_storage();
    itemset = NEW2(nitems, short);
    ruleset = NEW2(WORDSIZE(nrules), unsigned);
    set_first_derives();
    initialize_states();

    while (this_state)
    {
	closure(this_state->items, this_state->nitems);
	save_reductions();
	new_itemsets();
	append_states();

	if (nshifts > 0)
	    save_shifts();

	this_state = this_state->next;
    }

    finalize_closure();
    free_storage();
}
Ejemplo n.º 3
0
	void FinalRelease()
	{
		if (!m_fFirstAssembly)
		{
			free_storage();
		}
		free(output_contents);
	}
Ejemplo n.º 4
0
static void
house_keeping (void)
{
#ifdef TCB_MEMHASH
  /* free malloc'd int values on the agent list */
  if (conf.list_agents)
    free_agent_list ();
#endif

  /* REVERSE DNS THREAD */
  pthread_mutex_lock (&gdns_thread.mutex);
  /* kill dns pthread */
  active_gdns = 0;
  free_holder (&holder);
  gdns_free_queue ();

  /* free uniqmap */
#if defined(TCB_BTREE) || defined(TCB_MEMHASH)
  /*for (module = 0; module < TOTAL_MODULES; module++) { */
  /*free_db_key (get_storage_metric (module, MTRC_UNIQMAP)); */
  /*} */
#endif
  free_storage ();

  pthread_mutex_unlock (&gdns_thread.mutex);

  /* DASHBOARD */
  if (dash && !conf.output_html) {
    free_dashboard (dash);
    reset_find ();
  }

  /* GEOLOCATION */
#ifdef HAVE_LIBGEOIP
  if (geo_location_data != NULL)
    GeoIP_delete (geo_location_data);
#endif

  /* LOGGER */
  free (logger);

  /* INVALID REQUESTS */
  if (conf.invalid_requests_log) {
    LOG_DEBUG (("Closing invalid requests log.\n"));
    invalid_log_close ();
  }

  /* CONFIGURATION */
  if (conf.debug_log) {
    LOG_DEBUG (("Bye.\n"));
    dbg_log_close ();
  }

  /* free colors */
  free_color_lists ();
  /* free cmd arguments */
  free_cmd_args ();
}
Ejemplo n.º 5
0
/* clean up the ctx space */
void pconf_finish(PCONF_CTX *ctx)
{
	if (!check_magic(ctx))
		return;

	if (ctx->f)
		fclose(ctx->f);

	free_storage(ctx);

	ctx->magic = 0;
}
Ejemplo n.º 6
0
void CZ80Assembler::FinalRelease()
{
	free_storage();

	if (curr_input_file) {
		free(curr_input_file);
	}
	curr_input_file = NULL;
	if (output_filename) {
		free(output_filename);
	}
	output_filename = NULL;
}
Ejemplo n.º 7
0
static void
house_keeping (void)
{
#if defined(TCB_BTREE) || defined(TCB_MEMHASH)
  GModule module;
#endif

#ifndef TCB_BTREE
  if (conf.list_agents)
    free_agent_list ();
#endif

  /* REVERSE DNS THREAD */
  pthread_mutex_lock (&gdns_thread.mutex);
  /* kill dns pthread */
  active_gdns = 0;
  free_holder (&holder);
  gdns_free_queue ();

  /* free uniqmap */
#if defined(TCB_BTREE) || defined(TCB_MEMHASH)
  for (module = 0; module < TOTAL_MODULES; module++) {
    free_db_key (get_storage_metric (module, MTRC_UNIQMAP));
  }
#endif
  free_storage ();

  pthread_mutex_unlock (&gdns_thread.mutex);

  /* DASHBOARD */
  if (dash && !conf.output_html) {
    free_dashboard (dash);
    reset_find ();
  }

  /* GEOLOCATION */
#ifdef HAVE_LIBGEOIP
  if (geo_location_data != NULL)
    GeoIP_delete (geo_location_data);
#endif

  /* LOGGER */
  free (logger);

  /* CONFIGURATION */
  if (conf.debug_log) {
    LOG_DEBUG (("Bye.\n"));
    dbg_log_close ();
  }
  free_cmd_args ();
}
Ejemplo n.º 8
0
static void
house_keeping (void)
{
  /* REVERSE DNS THREAD */
  pthread_mutex_lock (&gdns_thread.mutex);
  /* kill dns pthread */
  active_gdns = 0;
  free_holder (&holder);
  gdns_free_queue ();
  if (ht_hostnames != NULL) {
#ifdef HAVE_LIBTOKYOCABINET
    tc_db_close (ht_hostnames, DB_HOSTNAMES);
#else
    g_hash_table_destroy (ht_hostnames);
#endif
  }
  pthread_mutex_unlock (&gdns_thread.mutex);

  /* DASHBOARD */
  if (dash && !conf.output_html) {
    free_dashboard (dash);
    reset_find ();
  }

  /* GEOLOCATION */
#ifdef HAVE_LIBGEOIP
  if (geo_location_data != NULL)
    GeoIP_delete (geo_location_data);
#endif

  /* STORAGE */
  free_storage ();

  /* LOGGER */
  free (logger);

  /* CONFIGURATION */
  if (conf.debug_log) {
    LOG_DEBUG (("Bye.\n"));
    dbg_log_close ();
  }
  free_cmd_args ();
}
Ejemplo n.º 9
0
void
generate_states (void)
{
  item_number initial_core = 0;
  state_list *list = NULL;
  allocate_storage ();
  new_closure (nritems);

  /* Create the initial state.  The 0 at the lhs is the index of the
     item of this initial rule.  */
  state_list_append (0, 1, &initial_core);

  /* States are queued when they are created; process them all.  */
  for (list = first_state; list; list = list->next)
    {
      state *s = list->state;
      if (trace_flag & trace_automaton)
	fprintf (stderr, "Processing state %d (reached by %s)\n",
		 s->number,
		 symbols[s->accessing_symbol]->tag);
      /* Set up itemset for the transitions out of this state.  itemset gets a
         vector of all the items that could be accepted next.  */
      closure (s->items, s->nitems);
      /* Record the reductions allowed out of this state.  */
      save_reductions (s);
      /* Find the itemsets of the states that shifts can reach.  */
      new_itemsets (s);
      /* Find or create the core structures for those states.  */
      append_states (s);

      /* Create the shifts structures for the shifts to those states,
	 now that the state numbers transitioning to are known.  */
      state_transitions_set (s, nshifts, shiftset);
    }

  /* discard various storage */
  free_closure ();
  free_storage ();

  /* Set up STATES. */
  set_states ();
}
Ejemplo n.º 10
0
int main()
{
    int n, m;
    int i;
    Employee *set = NULL;
 
    scanf("%d", &n);
    init_storage(&set, n);
    for ( i = 0 ; i < n ; i++ )
        scanf("%d %s %s %d", &(set[i].id), set[i].first_name, set[i].last_name, &(set[i].boss_id));
    char first_name_A[32], first_name_B[32];
    char last_name_A[32], last_name_B[32];
    Employee *A, *B;
    int type;
 
    scanf("%d", &m);
    for ( i = 0 ; i < m ; i++ )
    {
        scanf("%s %s", first_name_A, last_name_A); 
        A = find_employee_by_name(set, n, first_name_A, last_name_A);
 
        scanf("%s %s", first_name_B, last_name_B); 
        B = find_employee_by_name(set, n, first_name_B, last_name_B);
 
        type = check_relationship(set, n, A, B);
        switch(type){
            case 1:
                printf("subordinate\n"); break;
            case 2:
                printf("supervisor\n"); break;
            case 3:
                printf("colleague\n"); break;
            default:
                printf("unrelated\n"); break;
        }
    }
    free_storage(&set);
    return 0;
}
Ejemplo n.º 11
0
/* compute the nondeterministic finite state machine (see state.h for details)
from the grammar.  */
void
generate_states()
{
  allocate_storage();
  initialize_closure(nitems);
  initialize_states();

  while (this_state)
    {
      /* Set up ruleset and itemset for the transitions out of this state.
         ruleset gets a 1 bit for each rule that could reduce now.
	 itemset gets a vector of all the items that could be accepted next.  */
      closure(this_state->items, this_state->nitems);
      /* record the reductions allowed out of this state */
      save_reductions();
      /* find the itemsets of the states that shifts can reach */
      new_itemsets();
      /* find or create the core structures for those states */
      append_states();

      /* create the shifts structures for the shifts to those states,
         now that the state numbers transitioning to are known */
      if (nshifts > 0)
        save_shifts();

      /* states are queued when they are created; process them all */
      this_state = this_state->next;
    }

  /* discard various storage */
  finalize_closure();
  free_storage();

  /* set up initial and final states as parser wants them */
  augment_automaton();
}
Ejemplo n.º 12
0
int main (int argc, char **argv)
{
	int curr_arg = 1;
	bool case_sensitive = false;
	bool is_storage_initialized = false;

	use_colors = true;
	extern WORD user_attributes;
	user_attributes = save_console_attributes ();
	atexit (restore_console_attributes_at_exit);

	//if there aren't enough args, show info
	if (argc < 2) {
		puts ("SPASM-ng Z80 Assembler by Spencer Putt and Don Straney");
		printf ("Version %s (built on %s @ %s)\n", SPASM_NG_VERSION, __DATE__, __TIME__);
#ifdef SPASM_NG_GITREV
		printf ("Git revision %s\n", SPASM_NG_GITREV);
#endif
#ifdef _M_X64
		puts ("64-bit Version");
#endif
#ifdef NO_APPSIGN
		printf ("\nApp signing is NOT available in this build of SPASM.\n");
#endif
		puts ("\nspasm [options] <input file> <output file>\n");
		puts ("Options:\n-E = Assemble eZ80 code\n-T = Generate code listing\n-C = Code counter mode\n-L = Symbol table mode\n-S = Stats mode\n-O = Don't write to output file");
		puts ("-I [directory] = Add include directory\n-A = Labels are cAse-sensitive\n-D<name>[=value] = Create a define 'name' [with 'value']");
		puts ("-N = Don't use colors for messages");
		puts ("-V <Expression> = Pipe expression directly into assembly");

#if defined(_DEBUG) && defined(WIN32)
		if (IsDebuggerPresent())
		{
			system("PAUSE");
		}
#endif
		return EXIT_NORMAL;
	}

	//init stuff
	mode = MODE_NORMAL;
	in_macro = 0;
	
	//otherwise, get any options
	curr_input_file = strdup("Commandline");
	char *starting_input_file = curr_input_file;

	while (curr_arg < argc) {
		if (argv[curr_arg][0] == '-'
#ifdef _WINDOWS
			|| argv[curr_arg][0] == '/'
#endif
			)
		{
			switch (argv[curr_arg][1])
			{
			//args for different modes
			case 'O':
				mode = mode & (~MODE_NORMAL);
				break;
			case 'T':
				mode |= MODE_LIST;
				break;
			case 'C':
				mode |= MODE_CODE_COUNTER;
				break;
			case 'L':
				mode |= MODE_SYMTABLE;
				break;
			case 'S':
				mode |= MODE_STATS;
				break;
			case 'E':
				mode |= MODE_EZ80;
				all_opcodes = opcode_list_ez80;
				break;
			//handle no-colors flag
			case 'N':
				use_colors = false;
				break;
			//handle include files too
			case 'I':
			{
				char *dir, *p;
				//make sure there's another argument after it for the include path
				if (strlen(argv[curr_arg]) > 2) {
					dir = strdup (&argv[curr_arg][2]);
				} else {
					if (curr_arg >= argc - 1) {
						printf ("%s used without include path\n", argv[curr_arg]);
						break;
					}
					
					dir = strdup (argv[++curr_arg]);
				}
				
				for (p = strtok (dir, ";,"); p; p = strtok (NULL, ";,")) {
					include_dirs = list_append (include_dirs, strdup(p));
				}
				free(dir);
				break;
			}
			//and the case-sensitive flag
			case 'A':
				case_sensitive = true;
				break;
			//handle adding defines
			case 'D':
			{
				char name[256];
				char *ptr;
				define_t *define;

				if (!is_storage_initialized)
				{
					init_storage();
					is_storage_initialized = true;
				}

				if (strlen (argv[curr_arg]) > 2) {
					ptr = &argv[curr_arg][2];
				} else {
					if (curr_arg >= argc - 1) {
						printf ("%s used without define name", argv[curr_arg]);
						break;
					}
					
					ptr = argv[++curr_arg];
				}

				read_expr (&ptr, name, "=");

				define = add_define (strdup (name), NULL);
				if (*skip_whitespace (++ptr) != '\0')
					define->contents = strdup (ptr);
				else
					set_define (define, "1", 1, false);
				break;
			}
			case 'V':
			{
				char *line;
				
				//check for something after -V
				if (strlen(argv[curr_arg]) > 2) {
					line = &argv[curr_arg][2];
				} else {
					//if not lets fail
					if (curr_arg >= argc - 1) {
						printf ("%s used without a line to assemble\n", argv[curr_arg]);
						return EXIT_FATAL_ERROR;
					}
					line = argv[++curr_arg];
				}
				
				mode |= MODE_COMMANDLINE;
				curr_input_file = strdup("-v");
				input_contents = (char *) malloc (strlen(line) + 1 + 2);
				output_filename = change_extension (curr_input_file, "bin");
					
				strcpy(input_contents, line);
				strcat(input_contents, "\n");
				break;
			}
			default:
				{
#ifndef _TEST
#ifdef _WINDOWS
					printf ("Unrecognized option %s\n", argv[curr_arg]);
#ifdef SPASM_NG_ENABLE_COM
					FreeConsole();
					return _AtlModule.WinMain(SW_HIDE);
#endif
#endif
#else
					printf ("Unrecognized option %s\n", argv[curr_arg]);
#endif
				}
				
			}

		} else {
			//if it's not a flag, then it must be a filename
			if (curr_input_file && (curr_input_file != starting_input_file) && !output_filename)
				output_filename = strdup(argv[curr_arg]);
			else if ((!curr_input_file) || (curr_input_file == starting_input_file))
				curr_input_file = strdup(argv[curr_arg]);

		}
		curr_arg++;
	}

	// Update case sensitivity settings
	set_case_sensitive (case_sensitive);
	
	//check on filenames
	if (!(mode & MODE_COMMANDLINE) && curr_input_file == starting_input_file) {
		puts ("No input file specified");
		free(starting_input_file);
		return EXIT_FATAL_ERROR;
	}

	if (curr_input_file != starting_input_file) {
		free(starting_input_file);
	}

	if (!output_filename) {
		output_filename = change_extension (curr_input_file, "bin");
	}

	if (!is_storage_initialized)
	{
		init_storage();
		is_storage_initialized = true;
	}
	output_contents = (unsigned char *) malloc(output_buf_size);
	ClearSPASMErrorSessions();

	int error = run_assembly();

	free(output_filename);
	output_filename = NULL;
	if (curr_input_file) {
		free(curr_input_file);
		curr_input_file = NULL;
	}
	if (include_dirs) {
		list_free(include_dirs, true, NULL);
	}

	free(output_contents);
	output_contents = NULL;
	ClearSPASMErrorSessions();
	free_storage();

#ifdef _WINDOWS
	_CrtDumpMemoryLeaks();
	if (IsDebuggerPresent())
	{
		system("PAUSE");
	}
#endif

	return error;
}
Ejemplo n.º 13
0
	STDMETHOD(Assemble)(VARIANT varInput, IStream **ppOutput)
	{
		mode = m_dwOptions;

		if (V_VT(&varInput) == VT_BSTR)
		{
			mode |= MODE_NORMAL | MODE_COMMANDLINE;
			mode &= ~MODE_LIST;

			if (m_fFirstAssembly)
			{
				init_storage();
			}

			CW2CT szInput(V_BSTR(&varInput));
			input_contents = strdup(szInput);

			curr_input_file = strdup("COM Interface");
		}
		else
		{
			mode &= ~MODE_COMMANDLINE;
			mode |= MODE_NORMAL;

			curr_input_file = strdup(m_bstrInputFile);
			output_filename = strdup(m_bstrOutputFile);

			if (!m_fFirstAssembly)
			{
				free_storage();
			}

			init_storage();
		}

		// Set up the include directories
		CComPtr<IUnknown> pEnumUnk;
		HRESULT hr = m_pDirectories->get__NewEnum(&pEnumUnk);

		CComQIPtr<IEnumVARIANT> pEnum = pEnumUnk;

		CComVariant varItem;
		ULONG ulFetched;

		while (pEnum->Next(1, &varItem, &ulFetched) == S_OK)
		{
			include_dirs = list_prepend(include_dirs, (char *) strdup(_bstr_t(V_BSTR(&varItem))));
		}

		AddDefines();

		int error = run_assembly();

		list_free(include_dirs, true, NULL);
		include_dirs = NULL;

		ClearSPASMErrorSessions();

		long assembled_size = out_ptr - output_contents;

		HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, assembled_size);
		unsigned char *streambuf = (unsigned char *)GlobalLock(hGlobal);
		memcpy(streambuf, output_contents, assembled_size);
		GlobalUnlock(hGlobal);

		CComPtr<IStream> pStream;
		hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
		ULARGE_INTEGER ul;
		ul.QuadPart = assembled_size;
		pStream->SetSize(ul);

		m_fFirstAssembly = FALSE;
		if (output_filename != NULL)
		{
			free(output_filename);
			output_filename = NULL;
		}
		if (curr_input_file)
		{
			free(curr_input_file);
			curr_input_file = NULL;
		}

		if (mode & MODE_COMMANDLINE)
		{
			free(input_contents);
			input_contents = NULL;
		}

		m_fLabelsAreValid = FALSE;
		return pStream->QueryInterface(ppOutput);
	}
Ejemplo n.º 14
0
int
call_function(ZARRAYP libID, const char *funcname, ZARRAYP argtypes, ZARRAYP args, ZARRAYP retval)
{
  /* Last value in argtypes and ffi_types is the type of "funcname" return value */ 
  int maxargs = argtypes->len - 1, nargs;
  ffi_cif cif;
  ffi_type *ffi_types[maxargs + 1]; 
  void *ffi_values[maxargs];

  int i, j;
  size_t fullsize = 0, size = 0;
  storage mem;
  init_storage(&mem);


  for (i = 0, j = 0; i < maxargs + 1; ++j, ++i) {
    size = get_size(argtypes->data[i]);
    ffi_types[j] = get_ffi_type(argtypes, &i, &mem);
    if (!ffi_types[j]) {
      return ZF_FAILURE;
    }
    if (ffi_types[j] == &ffi_type_void && i != maxargs) {
      logger("CNA_VOID type may be used only for return value\n");
      return ZF_FAILURE;
    }
    if (size == 0 && ffi_types[j] != &ffi_type_void && i != maxargs) {
      size = *((size_t *)(args->data + fullsize));
      fullsize += sizeof(size_t);
    }
    if (i != maxargs) {
      ffi_values[j] = args->data + fullsize;
    }
    fullsize += size;
  }

  retval->len = size;
  nargs = j - 1;

  if (fullsize != args->len + retval->len) {
    logger("Wrong size of ZARRAYP\n\tfullsize: %u\tZARRAYP args: %u\tretsize: %u\n", fullsize, args->len, retval->len);
    return ZF_FAILURE;
  }

  void *handle;
  if (assign_ZARRAYP_to_pointer(&handle, libID)) {
    return ZF_FAILURE;
  }

  if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, ffi_types[nargs], ffi_types) != FFI_OK) {
    logger("ffi_prep_cif() failed\n");
    return ZF_FAILURE;
  }
  
  if (retval->len == 0 && ffi_types[nargs] != &ffi_type_void) {
    retval->len = cif.arg_types[nargs]->size;
  }

  void *funcpointer = FIND_ENTRY(handle, funcname);

  if (!funcpointer) {
    logger("FIND_ENTRY() failed\n\thandle:%d\tfuncname:%s\n", handle, funcname);
    return ZF_FAILURE;
  }

  ffi_call(&cif, funcpointer, retval->data, ffi_values);

  /* TODO: handle error */

  free_storage(&mem);

  return ZF_SUCCESS;
}