예제 #1
0
void remove_pcr_duplicates(BaseQuality& base_quality, bool use_bam_rgs,
			   std::map<std::string, std::string>& rg_to_library,
			   std::vector< std::vector<BamTools::BamAlignment> >& paired_strs_by_rg,
			   std::vector< std::vector<BamTools::BamAlignment> >& mate_pairs_by_rg,
			   std::vector< std::vector<BamTools::BamAlignment> >& unpaired_strs_by_rg, std::ostream& logger){
  int32_t dup_count = 0;
  assert(paired_strs_by_rg.size() == mate_pairs_by_rg.size() && paired_strs_by_rg.size() == unpaired_strs_by_rg.size());
  for (unsigned int i = 0; i < paired_strs_by_rg.size(); i++){
    assert(paired_strs_by_rg[i].size() == mate_pairs_by_rg[i].size());

    std::vector<ReadPair> read_pairs;
    for (unsigned int j = 0; j < paired_strs_by_rg[i].size(); j++){
      std::string library = use_bam_rgs ? get_library(paired_strs_by_rg[i][j], rg_to_library): rg_to_library[paired_strs_by_rg[i][j].Filename];
      read_pairs.push_back(ReadPair(paired_strs_by_rg[i][j], mate_pairs_by_rg[i][j], library));
    }
    for (unsigned int j = 0; j < unpaired_strs_by_rg[i].size(); j++){
      std::string library = use_bam_rgs ? get_library(unpaired_strs_by_rg[i][j], rg_to_library): rg_to_library[unpaired_strs_by_rg[i][j].Filename];
      read_pairs.push_back(ReadPair(unpaired_strs_by_rg[i][j], library));
    }
    std::sort(read_pairs.begin(), read_pairs.end());

    paired_strs_by_rg[i].clear();
    mate_pairs_by_rg[i].clear();
    unpaired_strs_by_rg[i].clear();
    if (read_pairs.size() == 0)
      continue;
    int best_index = 0;
    for (unsigned int j = 1; j < read_pairs.size(); j++){
      if (read_pairs[j].duplicate(read_pairs[best_index])){
	dup_count++;
	// Update index if new pair's STR read has a higher total base quality
	if (base_quality.sum_log_prob_correct(read_pairs[j].aln_one().Qualities) > 
	    base_quality.sum_log_prob_correct(read_pairs[best_index].aln_one().Qualities))
	  best_index = j;
      }
      else {
	// Keep best pair from prior set of duplicates
	if (read_pairs[best_index].single_ended())
	  unpaired_strs_by_rg[i].push_back(read_pairs[best_index].aln_one());
	else {
	  paired_strs_by_rg[i].push_back(read_pairs[best_index].aln_one());
	  mate_pairs_by_rg[i].push_back(read_pairs[best_index].aln_two());
	}
	best_index = j; // Update index for new set of duplicates
      }
    }

    // Keep best pair for last set of duplicates
    if (read_pairs[best_index].single_ended())
      unpaired_strs_by_rg[i].push_back(read_pairs[best_index].aln_one());
    else {
      paired_strs_by_rg[i].push_back(read_pairs[best_index].aln_one());
      mate_pairs_by_rg[i].push_back(read_pairs[best_index].aln_two());
    }
  }
  logger << "Removed " << dup_count << " sets of PCR duplicate reads" << std::endl;
}
예제 #2
0
파일: ccall.cpp 프로젝트: BigCrunsh/julia
extern "C" DLLEXPORT
void *jl_load_and_lookup(char *f_lib, char *f_name, uv_lib_t **hnd)
{
    uv_lib_t *handle = *hnd;
    if (!handle)
        *hnd = handle = get_library(f_lib);
    void *ptr = jl_dlsym_e(handle, f_name);
    if (!ptr)
        jl_errorf("symbol could not be found %s: %s\n", f_name, uv_dlerror(handle));
    return ptr;
}
예제 #3
0
Ref<Script> NativeScript::get_base_script() const {
	NativeScriptDesc *script_data = get_script_desc();

	if (!script_data)
		return Ref<Script>();

	Ref<NativeScript> ns = Ref<NativeScript>(NSL->create_script());
	ns->set_class_name(script_data->base);
	ns->set_library(get_library());
	return ns;
}
예제 #4
0
파일: main.c 프로젝트: masums/uia2atk
void
gnome_accessibility_module_shutdown ()
{
  void *library = get_library ();
  void_func func;

  if (!library)
    return;
  func = dlsym (library, "gnome_accessibility_module_shutdown");
  if (func)
    func ();
  else
    g_warning ("libbridgeglue: Couldn't find gnome_accessibility_module_shutdown");
}
예제 #5
0
파일: nativescript.cpp 프로젝트: 93i/godot
Ref<Script> NativeScript::get_base_script() const {
	NativeScriptDesc *script_data = get_script_desc();

	if (!script_data)
		return Ref<Script>();

	NativeScript *script = (NativeScript *)NSL->create_script();
	Ref<NativeScript> ns = Ref<NativeScript>(script);
	ERR_FAIL_COND_V(!ns.is_valid(), Ref<Script>());

	ns->set_class_name(script_data->base);
	ns->set_library(get_library());
	return ns;
}
예제 #6
0
bool GDNative::initialize() {
	if (library.is_null()) {
		ERR_PRINT("No library set, can't initialize GDNative object");
		return false;
	}

	String lib_path = library->get_active_library_path();
	if (lib_path.empty()) {
		ERR_PRINT("No library set for this platform");
		return false;
	}
#ifdef IPHONE_ENABLED
	String path = lib_path.replace("res://", "dylibs/");
#else
	String path = ProjectSettings::get_singleton()->globalize_path(lib_path);
#endif
	Error err = OS::get_singleton()->open_dynamic_library(path, native_handle);
	if (err != OK) {
		return false;
	}

	void *library_init;
	err = get_symbol(init_symbol, library_init);

	if (err || !library_init) {
		OS::get_singleton()->close_dynamic_library(native_handle);
		native_handle = NULL;
		ERR_PRINT("Failed to obtain godot_gdnative_init symbol");
		return false;
	}

	godot_gdnative_init_fn library_init_fpointer;
	library_init_fpointer = (godot_gdnative_init_fn)library_init;

	godot_gdnative_init_options options;

	options.api_struct = &api_struct;
	options.in_editor = Engine::get_singleton()->is_editor_hint();
	options.core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
	options.editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
	options.no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE);
	options.gd_native_library = (godot_object *)(get_library().ptr());
	options.active_library_path = (godot_string *)&path;

	library_init_fpointer(&options);

	return true;
}
예제 #7
0
파일: ccall.cpp 프로젝트: BigCrunsh/julia
static Value *runtime_sym_lookup(PointerType *funcptype, char *f_lib, char *f_name, jl_codectx_t *ctx)
{
    // in pseudo-code, this function emits the following:
    //   global uv_lib_t **libptrgv
    //   global void **llvmgv
    //   if (*llvmgv == NULL) {
    //       *llvmgv = jl_load_and_lookup(f_lib, f_name, libptrgv);
    //   }
    //   return (*llvmgv)
    Constant *initnul = ConstantPointerNull::get((PointerType*)T_pint8);

    uv_lib_t *libsym = NULL;
    bool runtime_lib = false;
    GlobalVariable *libptrgv;
#ifdef _OS_WINDOWS_
    if ((intptr_t)f_lib == 1) {
        libptrgv = prepare_global(jlexe_var);
        libsym = jl_exe_handle;
    }
    else if ((intptr_t)f_lib == 2) {
        libptrgv = prepare_global(jldll_var);
        libsym = jl_dl_handle;
    }
    else
#endif
    if (f_lib == NULL) {
        libptrgv = prepare_global(jlRTLD_DEFAULT_var);
        libsym = jl_RTLD_DEFAULT_handle;
    }
    else {
        runtime_lib = true;
        libptrgv = libMapGV[f_lib];
        if (libptrgv == NULL) {
            libptrgv = new GlobalVariable(*jl_Module, T_pint8,
               false, GlobalVariable::PrivateLinkage,
               initnul, f_lib);
            libMapGV[f_lib] = libptrgv;
            libsym = get_library(f_lib);
            assert(libsym != NULL);
#ifdef USE_MCJIT
            llvm_to_jl_value[libptrgv] = libsym;
#else
            *((uv_lib_t**)jl_ExecutionEngine->getPointerToGlobal(libptrgv)) = libsym;
#endif
        }
    }
    if (libsym == NULL) {
#ifdef USE_MCJIT
        libsym = (uv_lib_t*)llvm_to_jl_value[libptrgv];
#else
        libsym = *((uv_lib_t**)jl_ExecutionEngine->getPointerToGlobal(libptrgv));
#endif
    }

    assert(libsym != NULL);

    GlobalVariable *llvmgv = symMapGV[f_name];
    if (llvmgv == NULL) {
        // MCJIT forces this to have external linkage eventually, so we would clobber
        // the symbol of the actual function.
        std::string name = f_name;
        name = "ccall_" + name;
        llvmgv = new GlobalVariable(*jl_Module, T_pint8,
           false, GlobalVariable::PrivateLinkage,
           initnul, name);
        symMapGV[f_name] = llvmgv;
#ifdef USE_MCJIT
        llvm_to_jl_value[llvmgv] = jl_dlsym_e(libsym, f_name);
#else
        *((void**)jl_ExecutionEngine->getPointerToGlobal(llvmgv)) = jl_dlsym_e(libsym, f_name);
#endif
    }

    BasicBlock *dlsym_lookup = BasicBlock::Create(getGlobalContext(), "dlsym"),
               *ccall_bb = BasicBlock::Create(getGlobalContext(), "ccall");
    builder.CreateCondBr(builder.CreateICmpNE(builder.CreateLoad(llvmgv), initnul), ccall_bb, dlsym_lookup);

    ctx->f->getBasicBlockList().push_back(dlsym_lookup);
    builder.SetInsertPoint(dlsym_lookup);
    Value *libname;
    if (runtime_lib) {
        libname = builder.CreateGlobalStringPtr(f_lib);
    }
    else {
        libname = literal_static_pointer_val(f_lib, T_pint8);
    }
    Value *llvmf = builder.CreateCall3(prepare_call(jldlsym_func), libname, builder.CreateGlobalStringPtr(f_name), libptrgv);
    builder.CreateStore(llvmf, llvmgv);
    builder.CreateBr(ccall_bb);

    ctx->f->getBasicBlockList().push_back(ccall_bb);
    builder.SetInsertPoint(ccall_bb);
    llvmf = builder.CreateLoad(llvmgv);
    return builder.CreatePointerCast(llvmf,funcptype);
}
예제 #8
0
int main(int argc, char *argv[]) {
    char cmdline[256];
    char *prefix;
    size_t prefix_len;
    int (*compfn)(const void *a, const void *b);

    pm_kernel_t *ker;
    pm_process_t *proc;

    pid_t *pids;
    size_t num_procs;

    pm_map_t **maps;
    size_t num_maps;
    pm_memusage_t map_usage;

    struct library_info *li, **lis;
    struct mapping_info *mi, **mis;
    struct process_info *pi;

    size_t i, j;
    int error;
    int perm;
    bool all;
    uint64_t required_flags;
    uint64_t flags_mask;

    bool has_swap = false;

    signal(SIGPIPE, SIG_IGN);
    compfn = &sort_by_pss;
    order = -1;
    prefix = NULL;
    prefix_len = 0;
    opterr = 0;
    perm = 0;
    all = false;
    required_flags = 0;
    flags_mask = 0;

    while (1) {
        int c;
        const struct option longopts[] = {
            {"all", 0, 0, 'a'},
            {"cached", 0, 0, 'c'},
            {"nocached", 0, 0, 'C'},
            {"ksm", 0, 0, 'k'},
            {"help", 0, 0, 'h'},
            {"pss", 0, 0, 'p'},
            {"uss", 0, 0, 'u'},
            {"vss", 0, 0, 'v'},
            {"rss", 0, 0, 'r'},
            {"swap", 0, 0, 's'},
            {"reverse", 0, 0, 'R'},
            {"path", required_argument, 0, 'P'},
            {"perm", required_argument, 0, 'm'},
            {0, 0, 0, 0}
        };
        c = getopt_long(argc, argv, "acChkm:pP:uvrsR", longopts, NULL);
        if (c < 0) {
            break;
        }
        /* Alphabetical cases */
        switch (c) {
        case 'a':
            all = true;
            break;
        case 'c':
            required_flags = 0;
            flags_mask = PM_PAGE_SWAPBACKED;
            break;
        case 'C':
            required_flags = PM_PAGE_SWAPBACKED;
            flags_mask = PM_PAGE_SWAPBACKED;
            break;
        case 'k':
            required_flags = PM_PAGE_KSM;
            flags_mask = PM_PAGE_KSM;
            break;
        case 'h':
            usage(argv[0]);
            exit(EXIT_SUCCESS);
        case 'm':
            perm = parse_perm(optarg);
            break;
        case 'p':
            compfn = &sort_by_pss;
            break;
        case 'P':
            prefix = optarg;
            prefix_len = strlen(prefix);
            break;
        case 'u':
            compfn = &sort_by_uss;
            break;
        case 'v':
            compfn = &sort_by_vss;
            break;
        case 'r':
            compfn = &sort_by_rss;
            break;
        case 's':
            compfn = &sort_by_swap;
            break;
        case 'R':
            order *= -1;
            break;
        case '?':
            fprintf(stderr, "Invalid argument \"%s\".\n", argv[optind - 1]);
            usage(argv[0]);
            exit(EXIT_FAILURE);
        default:
            abort();
        }
    }

    argc -= optind;
    argv += optind;

    libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *));
    libraries_count = 0; libraries_size = INIT_LIBRARIES;

    error = pm_kernel_create(&ker);
    if (error) {
        fprintf(stderr, "Error initializing kernel interface -- "
                        "does this kernel have pagemap?\n");
        exit(EXIT_FAILURE);
    }

    error = pm_kernel_pids(ker, &pids, &num_procs);
    if (error) {
        fprintf(stderr, "Error listing processes.\n");
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < num_procs; i++) {
        error = pm_process_create(ker, pids[i], &proc);
        if (error) {
            fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
            continue;
        }

        pi = get_process(pids[i]);

        error = pm_process_maps(proc, &maps, &num_maps);
        if (error) {
            fprintf(stderr, "Error listing maps for process %d.\n", proc->pid);
            exit(EXIT_FAILURE);
        }

        for (j = 0; j < num_maps; j++) {
            if (prefix && (strncmp(pm_map_name(maps[j]), prefix, prefix_len)))
                continue;

            if (perm && (pm_map_flags(maps[j]) & PM_MAP_PERMISSIONS) != perm)
                continue;

            li = get_library(pm_map_name(maps[j]), all);
            if (!li)
                continue;

            mi = get_mapping(li, pi);

            error = pm_map_usage_flags(maps[j], &map_usage, flags_mask,
                                       required_flags);
            if (error) {
                fprintf(stderr, "Error getting map memory usage of "
                                "map %s in process %d.\n",
                        pm_map_name(maps[j]), proc->pid);
                exit(EXIT_FAILURE);
            }

            if (map_usage.swap) {
                has_swap = true;
            }

            pm_memusage_add(&mi->usage, &map_usage);
            pm_memusage_add(&li->total_usage, &map_usage);
        }
    }

    printf(" %6s   %6s   %6s   %6s   %6s  ", "RSStot", "VSS", "RSS", "PSS", "USS");

    if (has_swap) {
        printf(" %6s  ", "Swap");
    }

    printf("Name/PID\n");
    fflush(stdout);

    qsort(libraries, libraries_count, sizeof(libraries[0]), &licmp);

    for (i = 0; i < libraries_count; i++) {
        li = libraries[i];

        printf("%6zdK   %6s   %6s   %6s   %6s  ", li->total_usage.pss / 1024, "", "", "", "");
        if (has_swap) {
            printf(" %6s  ", "");
        }
        printf("%s\n", li->name);
        fflush(stdout);

        qsort(li->mappings, li->mappings_count, sizeof(li->mappings[0]), compfn);

        for (j = 0; j < li->mappings_count; j++) {
            mi = li->mappings[j];
            pi = mi->proc;
            printf(   " %6s  %6zdK  %6zdK  %6zdK  %6zdK  ", "",
                mi->usage.vss / 1024,
                mi->usage.rss / 1024,
                mi->usage.pss / 1024,
                mi->usage.uss / 1024);
            if (has_swap) {
                printf("%6zdK  ", mi->usage.swap / 1024);
            }
            printf("  %s [%d]\n",
                pi->cmdline,
                pi->pid);
        }
        printf("\n");
        fflush(stdout);
    }

    return 0;
}
예제 #9
0
파일: ccall.cpp 프로젝트: buybackoff/julia
static Value *runtime_sym_lookup(PointerType *funcptype, char *f_lib, char *f_name, jl_codectx_t *ctx)
{
    // in pseudo-code, this function emits the following:
    //   global uv_lib_t **libptrgv
    //   global void **llvmgv
    //   if (*llvmgv == NULL) {
    //       *llvmgv = jl_load_and_lookup(f_lib, f_name, libptrgv);
    //   }
    //   return (*llvmgv)
    Constant *initnul = ConstantPointerNull::get((PointerType*)T_pint8);

    uv_lib_t *libsym = NULL;
    bool runtime_lib = false;
    GlobalVariable *libptrgv;
#ifdef _OS_WINDOWS_
    if ((intptr_t)f_lib == 1)
        libptrgv = jlexe_var;
    else if ((intptr_t)f_lib == 2)
        libptrgv = jldll_var;
    else
#endif
    if (f_lib == NULL) {
        libptrgv = jlRTLD_DEFAULT_var;
    }
    else {
        runtime_lib = true;
        libptrgv = libMapGV[f_lib];
        if (libptrgv == NULL) {
            libptrgv = new GlobalVariable(*jl_Module, T_pint8,
               false, GlobalVariable::PrivateLinkage,
               initnul, f_lib);
            libMapGV[f_lib] = libptrgv;
            libsym = get_library(f_lib);
            *((uv_lib_t**)jl_ExecutionEngine->getPointerToGlobal(libptrgv)) = libsym;
        }
    }
    if (libsym == NULL) {
        libsym = *((uv_lib_t**)jl_ExecutionEngine->getPointerToGlobal(libptrgv));
    }

    GlobalVariable *llvmgv = symMapGV[f_name];
    if (llvmgv == NULL) {
        llvmgv = new GlobalVariable(*jl_Module, T_pint8,
           false, GlobalVariable::PrivateLinkage,
           initnul, f_name);
        symMapGV[f_name] = llvmgv;
        *((void**)jl_ExecutionEngine->getPointerToGlobal(llvmgv)) = jl_dlsym_e(libsym, f_name);
    }

    BasicBlock *dlsym_lookup = BasicBlock::Create(getGlobalContext(), "dlsym"),
               *ccall_bb = BasicBlock::Create(getGlobalContext(), "ccall");
    builder.CreateCondBr(builder.CreateICmpNE(builder.CreateLoad(llvmgv), initnul), ccall_bb, dlsym_lookup);

    ctx->f->getBasicBlockList().push_back(dlsym_lookup);
    builder.SetInsertPoint(dlsym_lookup);
    Value *libname;
    if (runtime_lib) {
        libname = builder.CreateGlobalStringPtr(f_lib);
    }
    else {
        libname = literal_static_pointer_val(f_lib, T_pint8);
    }
    Value *llvmf = builder.CreateCall3(jldlsym_func, libname, builder.CreateGlobalStringPtr(f_name), libptrgv);
    builder.CreateStore(llvmf, llvmgv);
    builder.CreateBr(ccall_bb);

    ctx->f->getBasicBlockList().push_back(ccall_bb);
    builder.SetInsertPoint(ccall_bb);
    llvmf = builder.CreateLoad(llvmgv);
    return builder.CreatePointerCast(llvmf,funcptype);
}
예제 #10
0
bool GDNative::initialize() {
	if (library.is_null()) {
		ERR_PRINT("No library set, can't initialize GDNative object");
		return false;
	}

	String lib_path = library->get_current_library_path();
	if (lib_path.empty()) {
		ERR_PRINT("No library set for this platform");
		return false;
	}
#ifdef IPHONE_ENABLED
	// on iOS we use static linking
	String path = "";
#elif defined(ANDROID_ENABLED)
	// On Android dynamic libraries are located separately from resource assets,
	// we should pass library name to dlopen(). The library name is flattened
	// during export.
	String path = lib_path.get_file();
#elif defined(UWP_ENABLED)
	// On UWP we use a relative path from the app
	String path = lib_path.replace("res://", "");
#elif defined(OSX_ENABLED)
	// On OSX the exported libraries are located under the Frameworks directory.
	// So we need to replace the library path.
	String path = ProjectSettings::get_singleton()->globalize_path(lib_path);
	if (!FileAccess::exists(path)) {
		path = OS::get_singleton()->get_executable_path().get_base_dir().plus_file("../Frameworks").plus_file(lib_path.get_file());
	}
#else
	String path = ProjectSettings::get_singleton()->globalize_path(lib_path);
#endif

	if (library->should_load_once()) {
		if (GDNativeLibrary::loaded_libraries->has(lib_path)) {
			// already loaded. Don't load again.
			// copy some of the stuff instead
			this->native_handle = (*GDNativeLibrary::loaded_libraries)[lib_path][0]->native_handle;
			initialized = true;
			return true;
		}
	}

	Error err = OS::get_singleton()->open_dynamic_library(path, native_handle, true);
	if (err != OK) {
		return false;
	}

	void *library_init;

	// we cheat here a little bit. you saw nothing
	initialized = true;

	err = get_symbol(library->get_symbol_prefix() + init_symbol, library_init, false);

	initialized = false;

	if (err || !library_init) {
		OS::get_singleton()->close_dynamic_library(native_handle);
		native_handle = NULL;
		ERR_PRINT("Failed to obtain godot_gdnative_init symbol");
		return false;
	}

	godot_gdnative_init_fn library_init_fpointer;
	library_init_fpointer = (godot_gdnative_init_fn)library_init;

	static uint64_t core_api_hash = 0;
	static uint64_t editor_api_hash = 0;
	static uint64_t no_api_hash = 0;

	if (!(core_api_hash || editor_api_hash || no_api_hash)) {
		core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
		editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
		no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE);
	}

	godot_gdnative_init_options options;

	options.api_struct = &api_struct;
	options.in_editor = Engine::get_singleton()->is_editor_hint();
	options.core_api_hash = core_api_hash;
	options.editor_api_hash = editor_api_hash;
	options.no_api_hash = no_api_hash;
	options.report_version_mismatch = &_gdnative_report_version_mismatch;
	options.report_loading_error = &_gdnative_report_loading_error;
	options.gd_native_library = (godot_object *)(get_library().ptr());
	options.active_library_path = (godot_string *)&path;

	library_init_fpointer(&options);

	initialized = true;

	if (library->should_load_once() && !GDNativeLibrary::loaded_libraries->has(lib_path)) {
		Vector<Ref<GDNative> > gdnatives;
		gdnatives.resize(1);
		gdnatives.write[0] = Ref<GDNative>(this);
		GDNativeLibrary::loaded_libraries->insert(lib_path, gdnatives);
	}

	return true;
}
예제 #11
0
int main(int argc, char *argv[]) {
    char cmdline[256];
    char *prefix;
    size_t prefix_len;
    int (*compfn)(const void *a, const void *b);
    /// M: get swap usage @{
    int show_swap;
    /// @}

    pm_kernel_t *ker;
    pm_process_t *proc;

    pid_t *pids;
    size_t num_procs;

    pm_map_t **maps;
    size_t num_maps;
    pm_memusage_t map_usage;

    struct library_info *li, **lis;
    struct mapping_info *mi, **mis;
    struct process_info *pi;

    int i, j, error;

    signal(SIGPIPE, SIG_IGN);
    compfn = &sort_by_pss;
    order = -1;
    prefix = NULL;
    prefix_len = 0;
    /// M: get swap usage @{
    show_swap = 0;
    /// @}

    for (i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "-P")) {
            if (i + 1 >= argc) {
                fprintf(stderr, "Option -P requires an argument.\n");
                usage(argv[0]);
                exit(EXIT_FAILURE);
            }
            prefix = argv[++i];
            prefix_len = strlen(prefix);
            continue;
        }
        if (!strcmp(argv[i], "-v")) { compfn = &sort_by_vss; continue; }
        if (!strcmp(argv[i], "-r")) { compfn = &sort_by_rss; continue; }
        if (!strcmp(argv[i], "-p")) { compfn = &sort_by_pss; continue; }
        if (!strcmp(argv[i], "-u")) { compfn = &sort_by_uss; continue; }
        if (!strcmp(argv[i], "-R")) { order *= -1; continue; }
        /// M: get swap usage @{
        if (!strcmp(argv[i], "-s")) { show_swap = 1; continue; }
        /// @}
        if (!strcmp(argv[i], "-h")) { usage(argv[0]); exit(0); }
        fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
        usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *));
    libraries_count = 0; libraries_size = INIT_LIBRARIES;

    error = pm_kernel_create(&ker);
    if (error) {
        fprintf(stderr, "Error initializing kernel interface -- "
                        "does this kernel have pagemap?\n");
        exit(EXIT_FAILURE);
    }

    error = pm_kernel_pids(ker, &pids, &num_procs);
    if (error) {
        fprintf(stderr, "Error listing processes.\n");
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < num_procs; i++) {
        error = pm_process_create(ker, pids[i], &proc);
        if (error) {
            fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
            continue;
        }
        
        pi = get_process(pids[i]);

        error = pm_process_maps(proc, &maps, &num_maps);
        if (error) {
            fprintf(stderr, "Error listing maps for process %d.\n", proc->pid);
            exit(EXIT_FAILURE);
        }

        for (j = 0; j < num_maps; j++) {
            if (prefix && (strncmp(pm_map_name(maps[j]), prefix, prefix_len)))
                continue;

            li = get_library(pm_map_name(maps[j]));
            if (!li)
                continue;

            mi = get_mapping(li, pi);
            
            error = pm_map_usage(maps[j], &map_usage);
            if (error) {
                fprintf(stderr, "Error getting map memory usage of "
                                "map %s in process %d.\n",
                        pm_map_name(maps[j]), proc->pid);
                exit(EXIT_FAILURE);
            }
            pm_memusage_add(&mi->usage, &map_usage);
            pm_memusage_add(&li->total_usage, &map_usage);
        }
    }

    /// M: get swap usage @{
    if (show_swap) {
        printf(          " %6s   %6s   %6s   %6s   %6s   %6s   %6s  %s\n", "RSStot", "VSS", "RSS", "PSS", "USS", "SWAP", "PSWAP", "Name/PID");
    } else {
        printf(          " %6s   %6s   %6s   %6s   %6s  %s\n", "RSStot", "VSS", "RSS", "PSS", "USS", "Name/PID");
    }
    /// @}
    fflush(stdout);

    qsort(libraries, libraries_count, sizeof(libraries[0]), &licmp);

    for (i = 0; i < libraries_count; i++) {
        li = libraries[i];

        printf("%6dK   %6s   %6s   %6s   %6s  %s\n", li->total_usage.pss / 1024, "", "", "", "", li->name);
        fflush(stdout);

        qsort(li->mappings, li->mappings_count, sizeof(li->mappings[0]), compfn);

        for (j = 0; j < li->mappings_count; j++) {
            mi = li->mappings[j];
            pi = mi->proc;
            /// M: get swap usage @{
            if (show_swap) {
                printf(   " %6s  %6dK  %6dK  %6dK  %6dK  %6dK  %6dK    %s [%d]\n", "",
                    mi->usage.vss / 1024,
                    mi->usage.rss / 1024,
                    mi->usage.pss / 1024,
                    mi->usage.uss / 1024,
                    mi->usage.swap / 1024,
                    mi->usage.pswap / 1024,
                    pi->cmdline,
                    pi->pid);
            } else {
                printf(   " %6s  %6dK  %6dK  %6dK  %6dK    %s [%d]\n", "",
                    mi->usage.vss / 1024,
                    mi->usage.rss / 1024,
                    mi->usage.pss / 1024,
                    mi->usage.uss / 1024,
                    pi->cmdline,
                    pi->pid);
            }
            /// @}
        }
        printf("\n");
        fflush(stdout);
    }

    return 0;
}