예제 #1
0
SSLStateMachine *SSLStateMachine_new(const char *szCertificateFile,
				     const char *szKeyFile)
    {
    SSLStateMachine *pMachine=malloc(sizeof *pMachine);
    int n;

    die_unless(pMachine);

    pMachine->pCtx=SSL_CTX_new(SSLv23_server_method());
    die_unless(pMachine->pCtx);

    n=SSL_CTX_use_certificate_file(pMachine->pCtx,szCertificateFile,
				   SSL_FILETYPE_PEM);
    die_unless(n > 0);

    n=SSL_CTX_use_PrivateKey_file(pMachine->pCtx,szKeyFile,SSL_FILETYPE_PEM);
    die_unless(n > 0);

    pMachine->pSSL=SSL_new(pMachine->pCtx);
    die_unless(pMachine->pSSL);

    pMachine->pbioRead=BIO_new(BIO_s_mem());

    pMachine->pbioWrite=BIO_new(BIO_s_mem());

    SSL_set_bio(pMachine->pSSL,pMachine->pbioRead,pMachine->pbioWrite);

    SSL_set_accept_state(pMachine->pSSL);

    return pMachine;
    }
예제 #2
0
int main()
{
#if STXXL_PARALLEL_MULTIWAY_MERGE
    LOG1 << "STXXL_PARALLEL_MULTIWAY_MERGE";
#endif
    // special parameter type
    using InputType = stxxl::stream::from_sorted_sequences<value_type>;
    using CreateRunsAlg = stxxl::stream::runs_creator<
              InputType, Cmp, 4096, foxxll::random_cyclic>;
    using SortedRunsType = CreateRunsAlg::sorted_runs_type;

    unsigned input_size = (10 * megabyte / sizeof(value_type));

    Cmp c;
    CreateRunsAlg SortedRuns(c, 10 * megabyte);
    value_type checksum_before(0);

    std::mt19937_64 randgen;
    std::uniform_int_distribution<unsigned> distr_value;

    for (unsigned cnt = input_size; cnt > 0; )
    {
        std::uniform_int_distribution<unsigned> distr_size(1, cnt);
        unsigned run_size = distr_size(randgen);        // random run length
        cnt -= run_size;
        LOG1 << "current run size: " << run_size;

        std::vector<unsigned> tmp(run_size);            // create temp storage for current run
        // fill with random numbers
        std::generate(tmp.begin(), tmp.end(), std::bind(distr_value, std::ref(randgen)) _STXXL_FORCE_SEQUENTIAL);
        std::sort(tmp.begin(), tmp.end(), c);           // sort
        for (unsigned j = 0; j < run_size; ++j)
        {
            checksum_before += tmp[j];
            SortedRuns.push(tmp[j]);                    // push sorted values to the current run
        }
        SortedRuns.finish();                            // finish current run
    }

    SortedRunsType Runs = SortedRuns.result();          // get sorted_runs data structure
    die_unless(check_sorted_runs(Runs, Cmp()));
    // merge the runs
    stxxl::stream::runs_merger<SortedRunsType, Cmp> merger(Runs, Cmp(), 10 * megabyte);
    stxxl::vector<value_type, 4, stxxl::lru_pager<8> > array;
    LOG1 << input_size << " " << Runs->elements;
    LOG1 << "checksum before: " << checksum_before;
    value_type checksum_after(0);
    for (unsigned i = 0; i < input_size; ++i)
    {
        checksum_after += *merger;
        array.push_back(*merger);
        ++merger;
    }
    LOG1 << "checksum after:  " << checksum_after;
    die_unless(stxxl::is_sorted(array.cbegin(), array.cend(), Cmp()));
    die_unless(checksum_before == checksum_after);
    die_unless(merger.empty());

    return 0;
}
예제 #3
0
ReadStreamPtr Hdfs3OpenReadStream(
    const std::string& _path, const common::Range& range) {

    std::string path = _path;
    // crop off hdfs://
    die_unless(common::StartsWith(path, "hdfs://"));
    path = path.substr(7);

    // split uri into host/path
    std::vector<std::string> splitted = common::Split(path, '/', 2);
    die_unless(splitted.size() == 2);

    // prepend root /
    splitted[1] = "/" + splitted[1];

    hdfsFS fs = Hdfs3FindConnection(splitted[0]);

    // construct file handler
    hdfsFile file = hdfsOpenFile(
        fs, splitted[1].c_str(), O_RDONLY, /* bufferSize */ 0,
        /* replication */ 0, /* blocksize */ 0);
    if (!file)
        die("Could not open HDFS file \"" << _path << "\": " << hdfsGetLastError());

    return tlx::make_counting<Hdfs3ReadStream>(
        fs, file, /* start_byte */ range.begin, /* byte_count */ range.size());
}
예제 #4
0
파일: parser.c 프로젝트: cage433/alisp
expression *parse_expression_from_string(char *text){
    List *l = parse_expressions_from_string(text);
    die_unless(l != NULL && l->cdr == NULL, "List should have a single element");
    expression *exp = l->car;
    my_free(l);
    return exp;
}
예제 #5
0
void mapper_slot_print(mapper_slot slot)
{
    printf("%s/%s", slot->signal->device->name, slot->signal->name);
    int i = 0;
    const char *key;
    char type;
    const void *val;
    int length;
    while (!mapper_slot_property_index(slot, i++, &key, &length, &type, &val)) {
        die_unless(val!=0, "returned zero value\n");

        // already printed these
        if (strcmp(key, "device_name")==0 || strcmp(key, "signal_name")==0)
            continue;

        if (length) {
            printf(", %s=", key);
            if (strncmp(key, "bound", 5)==0) {
                int bound = *(int*)val;
                if (bound > 0 && bound < NUM_MAPPER_BOUNDARY_ACTIONS)
                    printf("%s", mapper_boundary_action_strings[bound]);
                else
                    printf("undefined");
            }
            else
                mapper_property_print(length, type, val);
        }
    }
}
예제 #6
0
void test(uint64_t data_mem, size_t memory_to_use, int seed)
{
    uint64_t records_to_sort = data_mem / sizeof(T);
    using vector_type = stxxl::vector<T, 2, stxxl::lru_pager<8>, block_size, alloc_strategy_type>;
    vector_type v(records_to_sort);

    using cmp = typename T::compare_less;

    size_t ndisks = foxxll::config::get_instance()->disks_number();
    LOG1 << "Sorting " << records_to_sort << " records of size " << sizeof(T);
    LOG1 << "Total volume " << (records_to_sort * sizeof(T)) / MB << " MiB";
    LOG1 << "Using " << memory_to_use / MB << " MiB";
    LOG1 << "Using " << ndisks << " disks";
    LOG1 << "Using " << alloc_strategy_type::name() << " allocation strategy ";
    LOG1 << "Block size " << vector_type::block_type::raw_size / 1024 << " KiB";

    LOG1 << "Filling vector...";
    random_fill_vector(v, [](uint64_t x) -> T { return T(static_cast<uint32_t>(x)); }, seed);

    LOG1 << "Sorting vector...";

    foxxll::stats_data before(*foxxll::stats::get_instance());

    stxxl::sort(v.begin(), v.end(), cmp(), memory_to_use);

    foxxll::stats_data after(*foxxll::stats::get_instance());

    LOG1 << "Checking order...";
    die_unless(stxxl::is_sorted(v.cbegin(), v.cend(), cmp()));

    LOG1 << "Sorting: " << (after - before);
    LOG1 << "Total:   " << *foxxll::stats::get_instance();
}
예제 #7
0
파일: testmonitor.c 프로젝트: EQ4/libmapper
void printlink(mapper_db_link link)
{
    printf("  %s -> %s", link->src_name, link->dest_name);

    int i=0;
    const char *key;
    char type;
    const void *val;
    int length;
    while(!mapper_db_link_property_index(link, i++, &key, &type,
                                         &val, &length))
    {
        die_unless(val!=0, "returned zero value\n");

        // already printed these
        if (strcmp(key, "src_name")==0
            || strcmp(key, "dest_name")==0)
            continue;

        if (length) {
            printf(", %s=", key);
            mapper_prop_pp(type, length, val);
        }
    }
    printf("\n");
}
예제 #8
0
파일: testmonitor.c 프로젝트: EQ4/libmapper
void printconnection(mapper_db_connection con)
{
    printf("  %s -> %s", con->src_name, con->dest_name);

    int i=0;
    const char *key;
    char type;
    const void *val;
    int length;
    while(!mapper_db_connection_property_index(con, i++, &key, &type,
                                               &val, &length))
    {
        die_unless(val!=0, "returned zero value\n");

        // already printed these
        if (strcmp(key, "src_name")==0
            || strcmp(key, "dest_name")==0)
            continue;

        if (length) {
            printf(", %s=", key);
            if (strcmp(key, "mode")==0)
                printf("%s", mode_strings[*((int*)val)]);
            else if (strncmp(key, "bound", 5)==0)
                printf("%s", bound_strings[*((int*)val)]);
            else
                mapper_prop_pp(type, length, val);
        }
    }
    printf("\n");
}
void test(size_t data_mem, size_t memory_to_use, uint64_t seed)
{
    size_t records_to_sort = data_mem / sizeof(T);
    using vector_type = stxxl::vector<T, 2, stxxl::lru_pager<8>, block_size, alloc_strategy_type>;

    memory_to_use = foxxll::div_ceil(memory_to_use, vector_type::block_type::raw_size) * vector_type::block_type::raw_size;

    vector_type v(records_to_sort);
    size_t ndisks = foxxll::config::get_instance()->disks_number();
    LOG1 << "Sorting " << records_to_sort << " records of size " << sizeof(T);
    LOG1 << "Total volume " << (records_to_sort * sizeof(T)) / MB << " MiB";
    LOG1 << "Using " << memory_to_use / MB << " MiB";
    LOG1 << "Using " << ndisks << " disks";
    LOG1 << "Using " << alloc_strategy_type::name() << " allocation strategy ";
    LOG1 << "Block size " << vector_type::block_type::raw_size / 1024 << " KiB";

    random_fill_vector(v, seed);

    LOG1 << "Sorting vector...";

    foxxll::stats_data before(*foxxll::stats::get_instance());

    using get_key = typename T::key_extract;
    stxxl::stable_ksort(v.begin(), v.end(), get_key(), memory_to_use);

    foxxll::stats_data after(*foxxll::stats::get_instance());

    LOG1 << "Checking order...";
    die_unless(stxxl::is_sorted(v.cbegin(), v.cend()));

    LOG1 << "Sorting: " << (after - before);
    LOG1 << "Total:   " << *foxxll::stats::get_instance();
}
예제 #10
0
    Hdfs3ReadStream(hdfsFS fs, hdfsFile file,
                    uint64_t start_byte, uint64_t /* byte_count */)
        : fs_(fs), file_(file) {

        int err = hdfsSeek(fs_, file_, start_byte);
        die_unless(err == 0);
    }
예제 #11
0
파일: testmonitor.c 프로젝트: EQ4/libmapper
void printsignal(mapper_db_signal sig)
{
    printf("  %s name=%s%s",
           sig->is_output ? "output" : "input",
           sig->device_name, sig->name);

    int i=0;
    const char *key;
    char type;
    const void *val;
    int length;
    while(!mapper_db_signal_property_index(sig, i++, &key, &type,
                                           &val, &length))
    {
        die_unless(val!=0, "returned zero value\n");

        // already printed these
        if (strcmp(key, "device_name")==0
            || strcmp(key, "name")==0
            || strcmp(key, "direction")==0)
            continue;

        if (length) {
            printf(", %s=", key);
            mapper_prop_pp(type, length, val);
        }
    }
    printf("\n");
}
예제 #12
0
파일: testmonitor.c 프로젝트: EQ4/libmapper
void printdevice(mapper_db_device dev)
{
    printf("  %s", dev->name);

    int i=0;
    const char *key;
    char type;
    const void *val;
    int length;
    while(!mapper_db_device_property_index(dev, i++, &key, &type,
                                           &val, &length))
    {
        die_unless(val!=0, "returned zero value\n");

        // already printed this
        if (strcmp(key, "name")==0)
            continue;
        if (strcmp(key, "synced")==0) {
            // check current time
            mapper_timetag_t now;
            mapper_monitor_now(mon, &now);
            mapper_timetag_t *tt = (mapper_timetag_t *)val;
            if (tt->sec == 0)
                printf(", seconds_since_sync=unknown");
            else
                printf(", seconds_since_sync=%f",
                       mapper_timetag_difference(now, *tt));
        }
        else if (length) {
            printf(", %s=", key);
            mapper_prop_pp(type, length, val);
        }
    }
    printf("\n");
}
예제 #13
0
파일: connection.c 프로젝트: EQ4/libmapper
const char *mapper_get_boundary_action_string(mapper_boundary_action bound)
{
    die_unless(bound < N_MAPPER_BOUNDARY_ACTIONS,
               "called mapper_get_boundary_action_string() with "
               "bad parameter.\n");

    return mapper_boundary_action_strings[bound];
}
예제 #14
0
const char *mapper_get_clipping_type_string(mapper_clipping_type clipping)
{
    die_unless(clipping < N_MAPPER_CLIPPING_TYPES && clipping >= 0,
               "called mapper_get_clipping_type_string() with "
               "bad parameter.\n");

    return mapper_clipping_type_strings[clipping];
}
예제 #15
0
파일: connection.c 프로젝트: EQ4/libmapper
const char *mapper_get_mode_type_string(mapper_mode_type mode)
{
    die_unless(mode < N_MAPPER_MODE_TYPES,
               "called mapper_get_mode_type_string() with "
               "bad parameter.\n");

    return mapper_mode_type_strings[mode];
}
예제 #16
0
int main()
{
    // Create a vector with 3 default Test objects (-1) and reserved space for 6 objects.
    // Content afterwards: {-1,-1,-1}
    stxxl::swap_vector<Test> vec(3, 6);

    // Push back 10 values from 0 to 9. Internally a swap resize will happen
    // Content afterwards: {-1,-1,-1,0,1,2,3,4,5,6,7,8,9}
    for (unsigned i = 0; i < 10; ++i) {
        Test a(i);
        vec.swap_back(a);
    }

    // Delete the third and the fourth object.
    // Content afterwards: {-1,-1,1,2,3,4,5,6,7,8,9}
    vec.erase(vec.begin() + 2, vec.begin() + 4);

    // Delete the sixth object.
    // Content afterwards: {-1,-1,1,2,3,5,6,7,8,9}
    vec.erase(vec.begin() + 5);

    // Check the values.
    int expected_vals[10] = { -1, -1, 1, 2, 3, 5, 6, 7, 8, 9 };
    die_unequal(vec.size(), 10u);
    for (unsigned i = 0; i < vec.size(); ++i) {
        die_unequal(vec[i].get_i(), expected_vals[i]);
    }

    // std::remove_if would fail because it makes use of copy assignment.
    // We test stxxl's swap_remove_if implementation instead.
    // STL: vec.erase(std::remove_if(vec.begin(), vec.end(), test_eraser()), vec.end());
    vec.erase(stxxl::swap_remove_if(vec.begin(), vec.end(), test_eraser()), vec.end());

    // Check the values.
    int expected_vals2[10] = { 1, 2, 3, 5, 6, 7, 9 };
    die_unequal(vec.size(), 7u);
    for (unsigned i = 0; i < vec.size(); ++i) {
        die_unequal(vec[i].get_i(), expected_vals2[i]);
    }

    // Clear the vector.
    vec.clear();
    die_unless(vec.empty());

    // Resize to 100 and overwrite the last value.
    // Content after resize and overwrite: {...,-1,100}
    // Note: clear() does not overwrite any values in the underlaying array.
    vec.resize(20);
    Test t(11);
    std::swap(vec[19], t);

    // Check the values.
    die_unequal(vec.size(), 20u);
    die_unequal(vec[19].get_i(), 11);

    return EXIT_SUCCESS;
}
예제 #17
0
void Hdfs3Glob(const std::string& _path, const GlobType& gtype,
               FileList& filelist) {

    std::string path = _path;
    // crop off hdfs://
    die_unless(common::StartsWith(path, "hdfs://"));
    path = path.substr(7);

    // split uri into host/path
    std::vector<std::string> splitted = common::Split(path, '/', 2);

    hdfsFS fs = Hdfs3FindConnection(splitted[0]);
    std::string hosturi = "hdfs://" + splitted[0];

    // prepend root /
    splitted[1] = "/" + splitted[1];

    // list directory
    int num_entries = 0;
    hdfsFileInfo* list = hdfsListDirectory(
        fs, splitted[1].c_str(), &num_entries);

    if (!list) return;

    for (int i = 0; i < num_entries; ++i) {
        FileInfo fi;

        fi.path = list[i].mName;
        // remove leading slashes
        while (fi.path.size() >= 2 && fi.path[0] == '/' && fi.path[1] == '/')
            fi.path.erase(fi.path.begin(), fi.path.begin() + 1);
        // prepend host uri
        fi.path = hosturi + fi.path;

        if (list[i].mKind == kObjectKindFile) {
            if (gtype == GlobType::All || gtype == GlobType::File) {
                // strangely full file name globs return the file with a / at
                // the end.
                while (fi.path.back() == '/')
                    fi.path.resize(fi.path.size() - 1);
                fi.type = Type::File;
                fi.size = list[i].mSize;
                filelist.emplace_back(fi);
            }
        }
        else if (list[i].mKind == kObjectKindDirectory) {
            if (gtype == GlobType::All || gtype == GlobType::Directory) {
                fi.type = Type::Directory;
                fi.size = list[i].mSize;
                filelist.emplace_back(fi);
            }
        }
    }

    hdfsFreeFileInfo(list, num_entries);
}
예제 #18
0
파일: frame.c 프로젝트: cage433/alisp
Hash *frame_create(List *args, List *values){
    Hash *frame = create_empty_frame();
    while (args != NULL && values != NULL){
        frame_add(frame, args->car, values->car);
        args = args->cdr;
        values = values->cdr;
    }
    die_unless(args == NULL && values == NULL, "Arg names and values have different lengths");
    return frame;
}
예제 #19
0
    tlx::CountingPtr<Subclass> GetOrDie(Id object_id) {
        auto it = map_.find(object_id);

        if (it != map_.end()) {
            die_unless(dynamic_cast<Subclass*>(it->second.get()));
            return tlx::CountingPtr<Subclass>(
                dynamic_cast<Subclass*>(it->second.get()));
        }

        die("object " + std::to_string(object_id) + " not in repository");
    }
예제 #20
0
int main(int argc, char* argv[])
{
    size_t nins;
    {
        die_verbose_if(argc < 2, "Usage: " << argv[0] << " #log_ins");
        const auto log_nins = foxxll::atoi64(argv[1]);
        die_verbose_if(log_nins > 31, "This test can't do more than 2^31 operations, you requested 2^" << log_nins);
        nins = 1ULL << log_nins;
    }

    // prepare random unique keys
    stxxl::vector<key_type> values(nins);
    std::mt19937_64 randgen;
    {
        random_fill_vector(values, randgen);

        LOG1 << "Sorting the random values";
        stxxl::sort(values.begin(), values.end(), comp_type(), 128 * 1024 * 1024);

        LOG1 << "Deleting duplicate values";
        {
            auto new_end = std::unique(values.begin(), values.end());
            values.resize(std::distance(values.begin(), new_end));
        }

        LOG1 << "Randomly permute input values";
        stxxl::shuffle(values.begin(), values.end(), randgen, 128 * 1024 * 1024);
    }

    btree_type BTree(1024 * 128, 1024 * 128);
    {
        LOG1 << "Inserting " << values.size() << " random values into btree";
        for (auto it = values.cbegin(); it != values.cend(); ++it)
            BTree.insert({ *it, static_cast<payload_type>(*it + 1) });
        LOG1 << "Number of elements in btree: " << BTree.size();
    }

    {
        LOG1 << "Searching " << values.size() << " existing elements and erasing them";
        for (auto it = values.cbegin(); it != values.cend(); ++it) {
            auto bIt = BTree.find(*it);

            die_unless(bIt != BTree.end());
            // erasing non-existent element
            die_unless(BTree.erase((*it) + 1) == 0);
            // erasing existing element
            die_unless(BTree.erase(*it) == 1);
            // checking it is not there
            die_unless(BTree.find(*it) == BTree.end());
            // trying to erase it again
            die_unless(BTree.erase(*it) == 0);
        }
    }

    die_unless(BTree.empty());

    LOG1 << "Test passed.";

    return 0;
}
예제 #21
0
void Multiplexer::Close() {
    std::unique_lock<std::mutex> lock(mutex_);

    if (!d_->stream_sets_.map().empty()) {
        LOG1 << "Multiplexer::Close()"
             << " remaining_streams=" << d_->stream_sets_.map().size();
        die_unless(d_->stream_sets_.map().empty());
    }

    // destroy all still open Streams
    d_->stream_sets_.map().clear();

    closed_ = true;
}
예제 #22
0
    tlx::CountingPtr<Subclass>
    GetOrCreate(Id object_id, Types&& ... construction) {
        auto it = map_.find(object_id);

        if (it != map_.end()) {
            die_unless(dynamic_cast<Subclass*>(it->second.get()));
            return tlx::CountingPtr<Subclass>(
                dynamic_cast<Subclass*>(it->second.get()));
        }

        // construct new object
        tlx::CountingPtr<Subclass> value = tlx::make_counting<Subclass>(
            std::forward<Types>(construction) ...);

        map_.insert(std::make_pair(object_id, ObjectPtr(value)));
        return value;
    }
예제 #23
0
파일: xpcload.cpp 프로젝트: ahiguti/ase
static int
xpcload_main(const char *name)
{
  const char *mozhome = BOOST_PP_STRINGIZE(USE_XPCOM);
  setenv("MOZILLA_FIVE_HOME", mozhome, 0);
  nsCOMPtr<nsIServiceManager> servman;
  nsresult rv = NS_InitXPCOM2(getter_AddRefs(servman), nsnull, nsnull);
  die_unless(NS_SUCCEEDED(rv), "XPCOM init");

  nsCOMPtr<nsISupports> obj = do_CreateInstance(name, &rv);
  if (NS_SUCCEEDED(rv)) {
    fprintf(stderr, "success\n");
  } else {
    fprintf(stderr, "failed\n");
    const char *errstr = dlerror();
    fprintf(stderr, "dlerror: %s\n", errstr);
  }

  rv = NS_ShutdownXPCOM(NULL);
  return 0;
}
예제 #24
0
void Multiplexer::OnMixStreamBlock(
    size_t peer, Connection& s, const StreamMultiplexerHeader& header,
    const MixStreamDataPtr& stream, PinnedByteBlockPtr&& bytes) {

    die_unless(d_->ongoing_requests_[peer] > 0);
    d_->ongoing_requests_[peer]--;

    sLOG << "Multiplexer::OnMixStreamBlock()"
         << "got block" << *bytes << "seq" << header.seq << "on" << s
         << "in MixStream" << header.stream_id
         << "from worker" << header.sender_worker;

    stream->OnStreamBlock(
        header.sender_worker, header.seq,
        Block(std::move(bytes).ReleasePin(), /* begin */ 0, header.size,
              header.first_item, header.num_items,
              header.typecode_verify));

    if (header.is_last_block)
        stream->OnStreamBlock(header.sender_worker, header.seq + 1, Block());

    AsyncReadMultiplexerHeader(peer, s);
}
예제 #25
0
void basic_test()
{
    using value_type = std::pair<int, int>;
    const size_t value_size = sizeof(value_type);

    const size_t n_values = 6000;
    const size_t n_tests = 3000;

    // make sure all changes will be buffered (*)
    const size_t buffer_size = n_values * (value_size + sizeof(int*));

    const size_t mem_to_sort = 32 * 1024 * 1024;

    const size_t subblock_raw_size = 4 * 1024;
    const size_t block_size = 4;
    using unordered_map = stxxl::unordered_map<int, int, hash_int, cmp,
                                               subblock_raw_size, block_size>;
    using iterator = unordered_map::iterator;
    using const_iterator = unordered_map::const_iterator;

    foxxll::stats_data stats_begin;

    unordered_map map;
    map.max_buffer_size(buffer_size);
    const unordered_map& cmap = map;

    // generate random values

    std::vector<value_type> values1(n_values);
    std::vector<value_type> values2(n_values);
    std::vector<value_type> values3(n_values / 2);

    {
        auto broadcast = [](uint64_t x) -> value_type {
                             return {
                                        x, x
                             };
                         };
        random_fill_vector(values1, broadcast);
        random_fill_vector(values2, broadcast);
        random_fill_vector(values3, broadcast);
    }

    // --- initial import
    std::cout << "Initial import...";
    stats_begin = *foxxll::stats::get_instance();

    die_unless(map.begin() == map.end());
    map.insert(values1.begin(), values1.end(), mem_to_sort);
    die_unless(map.begin() != map.end());
    die_unless(map.size() == n_values);

    std::cout << "passed" << std::endl;
    LOG1 << foxxll::stats_data(*foxxll::stats::get_instance()) - stats_begin;

    // (*) all these values are stored in external memory; the remaining
    // changes will be buffered in internal memory

    // --- insert: new (from values2) and existing (from values1) values, with
    // --- and without checking
    std::cout << "Insert...";
    stats_begin = *foxxll::stats::get_instance();

    for (size_t i = 0; i < n_values / 2; i++) {
        // new without checking
        map.insert_oblivious(values2[2 * i]);
        // new with checking
        std::pair<iterator, bool> res = map.insert(values2[2 * i + 1]);
        die_unless(res.second && (*(res.first)).first == values2[2 * i + 1].first);
        // existing without checking
        map.insert_oblivious(values1[2 * i]);
        // exiting with checking
        res = map.insert(values1[2 * i + 1]);
        die_unless(!res.second && (*(res.first)).first == values1[2 * i + 1].first);
    }

    die_unless(map.size() == 2 * n_values);
    std::cout << "passed" << std::endl;
    LOG1 << foxxll::stats_data(*foxxll::stats::get_instance()) - stats_begin;

    // "old" values are stored in external memory, "new" values are stored in
    // internal memory

    // --- find: existing (from external and internal memory) and non-existing
    // --- values
    std::cout << "Find...";
    stats_begin = *foxxll::stats::get_instance();

    std::random_shuffle(values1.begin(), values1.end());
    std::random_shuffle(values2.begin(), values2.end());
    for (size_t i = 0; i < n_tests; i++) {
        die_unless(cmap.find(values1[i].first) != cmap.end());
        die_unless(cmap.find(values2[i].first) != cmap.end());
        die_unless(cmap.find(values3[i].first) == cmap.end());
    }
    std::cout << "passed" << std::endl;
    LOG1 << foxxll::stats_data(*foxxll::stats::get_instance()) - stats_begin;

    // --- insert with overwriting
    std::cout << "Insert with overwriting...";
    stats_begin = *foxxll::stats::get_instance();

    std::random_shuffle(values1.begin(), values1.end());
    std::random_shuffle(values2.begin(), values2.end());
    for (size_t i = 0; i < n_tests; i++) {
        value_type value1 = values1[i];         // in external memory
        value1.second++;
        map.insert_oblivious(value1);

        value_type value2 = values2[i];         // in internal memory
        value2.second++;
        map.insert_oblivious(value2);
    }
    // now check
    die_unless(map.size() == 2 * n_values);         // nothing added, nothing removed
    for (size_t i = 0; i < n_tests; i++) {
        const_iterator it1 = cmap.find(values1[i].first);
        const_iterator it2 = cmap.find(values2[i].first);

        die_unless((*it1).second == values1[i].second + 1);
        die_unless((*it2).second == values2[i].second + 1);
    }
    std::cout << "passed" << std::endl;
    LOG1 << foxxll::stats_data(*foxxll::stats::get_instance()) - stats_begin;

    // --- erase: existing and non-existing values, with and without checking
    std::cout << "Erase...";
    stats_begin = *foxxll::stats::get_instance();

    std::random_shuffle(values1.begin(), values1.end());
    std::random_shuffle(values2.begin(), values2.end());
    std::random_shuffle(values3.begin(), values3.end());
    for (size_t i = 0; i < n_tests / 2; i++) {        // external
        // existing without checking
        map.erase_oblivious(values1[2 * i].first);
        // existing with checking
        die_unless(map.erase(values1[2 * i + 1].first) == 1);
    }
    for (size_t i = 0; i < n_tests / 2; i++) {        // internal
        // existing without checking
        map.erase_oblivious(values2[2 * i].first);
        // existing with checking
        die_unless(map.erase(values2[2 * i + 1].first) == 1);
        // non-existing without checking
        map.erase_oblivious(values3[i].first);
        // non-existing with checking
    }
    die_unless(map.size() == 2 * n_values - 2 * n_tests);
    std::cout << "passed" << std::endl;
    LOG1 << foxxll::stats_data(*foxxll::stats::get_instance()) - stats_begin;

    map.clear();
    die_unless(map.size() == 0);

    // --- find and manipulate values by []-operator

    // make sure there are some values in our unordered_map: externally
    // [0..n/2) and internally [n/2..n) from values1
    std::cout << "[ ]-operator...";
    stats_begin = *foxxll::stats::get_instance();

    map.insert(values1.begin(), values1.begin() + n_values / 2, mem_to_sort);
    for (size_t i = n_values / 2; i < n_values; i++) {
        map.insert_oblivious(values1[i]);
    }
    // lookup of existing values
    die_unless(map[values1[5].first] == values1[5].second);                               // external
    die_unless(map[values1[n_values / 2 + 5].first] == values1[n_values / 2 + 5].second); // internal
    // manipulate existing values
    ++(map[values1[7].first]);
    ++(map[values1[n_values / 2 + 7].first]);
    {
        const_iterator cit1 = cmap.find(values1[7].first);
        die_unless((*cit1).second == (*cit1).first + 1);
        const_iterator cit2 = cmap.find(values1[n_values / 2 + 7].first);
        die_unless((*cit2).second == (*cit2).first + 1);
    }
    // lookup of non-existing values
    die_unless(map[values2[5].first] == unordered_map::mapped_type());
    // assignment of non-existing values
    map[values2[7].first] = values2[7].second;
    {
        const_iterator cit = cmap.find(values2[7].first);
        die_unless((*cit).first == values2[7].second);
    }
    die_unless(map.size() == n_values + 2);
    std::cout << "passed" << std::endl;
    LOG1 << foxxll::stats_data(*foxxll::stats::get_instance()) - stats_begin;

    map.clear();
    die_unless(map.size() == 0);

    // --- additional bulk insert test
    std::cout << "additional bulk-insert...";
    stats_begin = *foxxll::stats::get_instance();

    map.insert(values1.begin(), values1.begin() + n_values / 2, mem_to_sort);
    map.insert(values1.begin() + n_values / 2, values1.end(), mem_to_sort);
    die_unless(map.size() == n_values);
    // lookup some random values
    std::random_shuffle(values1.begin(), values1.end());
    for (size_t i = 0; i < n_tests; i++)
        die_unless(cmap.find(values1[i].first) != cmap.end());
    std::cout << "passed" << std::endl;
    LOG1 << foxxll::stats_data(*foxxll::stats::get_instance()) - stats_begin;

    // --- test equality predicate
    unordered_map::key_equal key_eq = map.key_eq();
    die_unless(key_eq(42, 42));
    die_unless(!key_eq(42, 6 * 9));

    std::cout << "\nAll tests passed" << std::endl;

    map.buffer_size();
}
예제 #26
0
파일: tls_drv.c 프로젝트: anwars99/ejabberd
static int tls_drv_control(ErlDrvData handle,
			   unsigned int command,
			   char *buf, int len,
			   char **rbuf, int rlen)
{
   tls_data *d = (tls_data *)handle;
   int res;
   int size;
   ErlDrvBinary *b;
   X509 *cert;
   unsigned int flags = command;

   command &= 0xffff;

   ERR_clear_error();
   switch (command)
   {
      case SET_CERTIFICATE_FILE_ACCEPT:
      case SET_CERTIFICATE_FILE_CONNECT: {
	 time_t mtime = 0;
	 SSL_CTX *ssl_ctx = hash_table_lookup(buf, &mtime);
	 if (is_key_file_modified(buf, &mtime) || ssl_ctx == NULL)
	 {
	    SSL_CTX *ctx;

	    hash_table_insert(buf, mtime, NULL);

	    ctx = SSL_CTX_new(SSLv23_method());
	    die_unless(ctx, "SSL_CTX_new failed");

	    res = SSL_CTX_use_certificate_chain_file(ctx, buf);
	    die_unless(res > 0, "SSL_CTX_use_certificate_file failed");

	    res = SSL_CTX_use_PrivateKey_file(ctx, buf, SSL_FILETYPE_PEM);
	    die_unless(res > 0, "SSL_CTX_use_PrivateKey_file failed");

	    res = SSL_CTX_check_private_key(ctx);
	    die_unless(res > 0, "SSL_CTX_check_private_key failed");

	    SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
	    SSL_CTX_set_default_verify_paths(ctx);
#ifdef SSL_MODE_RELEASE_BUFFERS
	    SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
#endif

	    if (command == SET_CERTIFICATE_FILE_ACCEPT)
	    {
	       SSL_CTX_set_verify(ctx,
				  SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
				  verify_callback);
	    }

	    ssl_ctx = ctx;
	    hash_table_insert(buf, mtime, ssl_ctx);
	 }

	 d->ssl = SSL_new(ssl_ctx);
	 die_unless(d->ssl, "SSL_new failed");

	 if (flags & VERIFY_NONE)
	    SSL_set_verify(d->ssl, SSL_VERIFY_NONE, verify_callback);

	 d->bio_read = BIO_new(BIO_s_mem());
	 d->bio_write = BIO_new(BIO_s_mem());

	 SSL_set_bio(d->ssl, d->bio_read, d->bio_write);

	 if (command == SET_CERTIFICATE_FILE_ACCEPT) {
	    SSL_set_options(d->ssl, SSL_OP_NO_TICKET);
	    SSL_set_accept_state(d->ssl);
	 } else {
	    SSL_set_options(d->ssl, SSL_OP_NO_SSLv2|SSL_OP_NO_TICKET);
	    SSL_set_connect_state(d->ssl);
	 }
	 break;
      }
      case SET_ENCRYPTED_INPUT:
	 die_unless(d->ssl, "SSL not initialized");
	 BIO_write(d->bio_read, buf, len);
	 break;
      case SET_DECRYPTED_OUTPUT:
	 die_unless(d->ssl, "SSL not initialized");
	 res = SSL_write(d->ssl, buf, len);
	 if (res <= 0) 
	 {
	    res = SSL_get_error(d->ssl, res);
	    if (res == SSL_ERROR_WANT_READ || res == SSL_ERROR_WANT_WRITE) 
	    {
	       b = driver_alloc_binary(1);
	       b->orig_bytes[0] = 2;
	       *rbuf = (char *)b;
	       return 1;
	    } else {
	       die_unless(0, "SSL_write failed");
	    }
	 }
	 break;
      case GET_ENCRYPTED_OUTPUT:
	 die_unless(d->ssl, "SSL not initialized");
	 size = BUF_SIZE + 1;
	 rlen = 1;
	 b = driver_alloc_binary(size);
	 b->orig_bytes[0] = 0;
	 while ((res = BIO_read(d->bio_write,
				b->orig_bytes + rlen, BUF_SIZE)) > 0)
	 {
	    //printf("%d bytes of encrypted data read from state machine\r\n", res);

	    rlen += res;
	    size += BUF_SIZE;
	    b = driver_realloc_binary(b, size);
	 }
	 b = driver_realloc_binary(b, rlen);
	 *rbuf = (char *)b;
	 return rlen;
      case GET_DECRYPTED_INPUT:
	 if (!SSL_is_init_finished(d->ssl))
	 {
	    res = SSL_do_handshake(d->ssl);
	    if (res <= 0)
	       die_unless(SSL_get_error(d->ssl, res) == SSL_ERROR_WANT_READ,
			  "SSL_do_handshake failed");
	 } else {
	    size = BUF_SIZE + 1;
	    rlen = 1;
	    b = driver_alloc_binary(size);
	    b->orig_bytes[0] = 0;

	    while ((res = SSL_read(d->ssl,
				   b->orig_bytes + rlen, BUF_SIZE)) > 0)
	    {
	       //printf("%d bytes of decrypted data read from state machine\r\n",res);
	       rlen += res;
	       size += BUF_SIZE;
	       b = driver_realloc_binary(b, size);
	    }

	    if (res < 0)
	    {
	       int err = SSL_get_error(d->ssl, res);

	       if (err == SSL_ERROR_WANT_READ)
	       {
		  //printf("SSL_read wants more data\r\n");
		  //return 0;
	       }
	       // TODO
	    }
	    b = driver_realloc_binary(b, rlen);
	    *rbuf = (char *)b;
	    return rlen;
	 }
	 break;
      case GET_PEER_CERTIFICATE:
	 cert = SSL_get_peer_certificate(d->ssl);
	 if (cert == NULL)
	 {
	    b = driver_alloc_binary(1);
	    b->orig_bytes[0] = 1;
	    *rbuf = (char *)b;
	    return 1;
	 } else {
	    unsigned char *tmp_buf;
	    rlen = i2d_X509(cert, NULL);
	    if (rlen >= 0)
	    {
	       rlen++;
	       b = driver_alloc_binary(rlen);
	       b->orig_bytes[0] = 0;
	       tmp_buf = (unsigned char *)&b->orig_bytes[1];
	       i2d_X509(cert, &tmp_buf);
	       X509_free(cert);
	       *rbuf = (char *)b;
	       return rlen;
	    } else
	       X509_free(cert);
	 }
	 break;
      case GET_VERIFY_RESULT:
	 b = driver_alloc_binary(1);
	 b->orig_bytes[0] = SSL_get_verify_result(d->ssl);
	 *rbuf = (char *)b;
	 return 1;
	 break;
   }

   b = driver_alloc_binary(1);
   b->orig_bytes[0] = 0;
   *rbuf = (char *)b;
   return 1;
}
예제 #27
0
파일: connection.c 프로젝트: EQ4/libmapper
int mapper_connection_perform(mapper_connection connection,
                              mapper_signal_history_t *from,
                              mapper_signal_history_t **expr_vars,
                              mapper_signal_history_t *to,
                              char *typestring)
{
    int changed = 0, i;
    int vector_length = from->length < to->length ? from->length : to->length;

    if (connection->props.muted)
        return 0;

    /* If the destination type is unknown, we can't do anything
     * intelligent here -- even bypass mode might screw up if we
     * assume the types work out. */
    if (connection->props.dest_type != 'f'
        && connection->props.dest_type != 'i'
        && connection->props.dest_type != 'd')
    {
        return 0;
    }

    if (!connection->props.mode || connection->props.mode == MO_BYPASS)
    {
        /* Increment index position of output data structure. */
        to->position = (to->position + 1) % to->size;
        if (connection->props.src_type == connection->props.dest_type) {
            memcpy(msig_history_value_pointer(*to),
                   msig_history_value_pointer(*from),
                   mapper_type_size(to->type) * vector_length);
            memset(msig_history_value_pointer(*to) +
                   mapper_type_size(to->type) * vector_length, 0,
                   (to->length - vector_length) * mapper_type_size(to->type));
        }
        else if (connection->props.src_type == 'f') {
            float *vfrom = msig_history_value_pointer(*from);
            if (connection->props.dest_type == 'i') {
                int *vto = msig_history_value_pointer(*to);
                for (i = 0; i < vector_length; i++) {
                    vto[i] = (int)vfrom[i];
                }
                for (; i < to->length; i++) {
                    vto[i] = 0;
                }
            }
            else if (connection->props.dest_type == 'd') {
                double *vto = msig_history_value_pointer(*to);
                for (i = 0; i < vector_length; i++) {
                    vto[i] = (double)vfrom[i];
                }
                for (; i < to->length; i++) {
                    vto[i] = 0;
                }
            }
        }
        else if (connection->props.src_type == 'i') {
            int *vfrom = msig_history_value_pointer(*from);
            if (connection->props.dest_type == 'f') {
                float *vto = msig_history_value_pointer(*to);
                for (i = 0; i < vector_length; i++) {
                    vto[i] = (float)vfrom[i];
                }
                for (; i < to->length; i++) {
                    vto[i] = 0;
                }
            }
            else if (connection->props.dest_type == 'd') {
                double *vto = msig_history_value_pointer(*to);
                for (i = 0; i < vector_length; i++) {
                    vto[i] = (double)vfrom[i];
                }
                for (; i < to->length; i++) {
                    vto[i] = 0;
                }
            }
        }
        else if (connection->props.src_type == 'd') {
            double *vfrom = msig_history_value_pointer(*from);
            if (connection->props.dest_type == 'i') {
                int *vto = msig_history_value_pointer(*to);
                for (i = 0; i < vector_length; i++) {
                    vto[i] = (int)vfrom[i];
                }
                for (; i < to->length; i++) {
                    vto[i] = 0;
                }
            }
            else if (connection->props.dest_type == 'f') {
                float *vto = msig_history_value_pointer(*to);
                for (i = 0; i < vector_length; i++) {
                    vto[i] = (float)vfrom[i];
                }
                for (; i < to->length; i++) {
                    vto[i] = 0;
                }
            }
        }
        for (i = 0; i < vector_length; i++) {
            typestring[i] = to->type;
        }
        return 1;
    }
    else if (connection->props.mode == MO_EXPRESSION
             || connection->props.mode == MO_LINEAR)
    {
        die_unless(connection->expr!=0, "Missing expression.\n");
        return (mapper_expr_evaluate(connection->expr, from, expr_vars,
                                     to, typestring));
    }
    else if (connection->props.mode == MO_CALIBRATE)
    {
        /* Increment index position of output data structure. */
        to->position = (to->position + 1) % to->size;

        if (!connection->props.src_min) {
            connection->props.src_min =
                malloc(connection->props.src_length *
                       mapper_type_size(connection->props.src_type));
        }
        if (!connection->props.src_max) {
            connection->props.src_max =
                malloc(connection->props.src_length *
                       mapper_type_size(connection->props.src_type));
        }

        /* If calibration mode has just taken effect, first data
         * sample sets source min and max */
        if (connection->props.src_type == 'f') {
            float *v = msig_history_value_pointer(*from);
            float *src_min = (float*)connection->props.src_min;
            float *src_max = (float*)connection->props.src_max;
            if (!connection->calibrating) {
                for (i = 0; i < from->length; i++) {
                    src_min[i] = v[i];
                    src_max[i] = v[i];
                }
                connection->calibrating = 1;
                changed = 1;
            }
            else {
                for (i = 0; i < from->length; i++) {
                    if (v[i] < src_min[i]) {
                        src_min[i] = v[i];
                        changed = 1;
                    }
                    if (v[i] > src_max[i]) {
                        src_max[i] = v[i];
                        changed = 1;
                    }
                }
            }
        }
        else if (connection->props.src_type == 'i') {
            int *v = msig_history_value_pointer(*from);
            int *src_min = (int*)connection->props.src_min;
            int *src_max = (int*)connection->props.src_max;
            if (!connection->calibrating) {
                for (i = 0; i < from->length; i++) {
                    src_min[i] = v[i];
                    src_max[i] = v[i];
                }
                connection->calibrating = 1;
                changed = 1;
            }
            else {
                for (i = 0; i < from->length; i++) {
                    if (v[i] < src_min[i]) {
                        src_min[i] = v[i];
                        changed = 1;
                    }
                    if (v[i] > src_max[i]) {
                        src_max[i] = v[i];
                        changed = 1;
                    }
                }
            }
        }
        else if (connection->props.src_type == 'd') {
            double *v = msig_history_value_pointer(*from);
            double *src_min = (double*)connection->props.src_min;
            double *src_max = (double*)connection->props.src_max;
            if (!connection->calibrating) {
                for (i = 0; i < from->length; i++) {
                    src_min[i] = v[i];
                    src_max[i] = v[i];
                }
                connection->calibrating = 1;
                changed = 1;
            }
            else {
                for (i = 0; i < from->length; i++) {
                    if (v[i] < src_min[i]) {
                        src_min[i] = v[i];
                        changed = 1;
                    }
                    if (v[i] > src_max[i]) {
                        src_max[i] = v[i];
                        changed = 1;
                    }
                }
            }
        }

        if (changed) {
            mapper_connection_set_mode_linear(connection);

            /* Stay in calibrate mode. */
            connection->props.mode = MO_CALIBRATE;
        }

        if (connection->expr)
            return (mapper_expr_evaluate(connection->expr, from, expr_vars,
                                         to, typestring));
        else
            return 0;
    }
    return 1;
}
예제 #28
0
int mapper_connection_perform(mapper_connection connection,
                              mapper_signal sig,
                              mapper_signal_value_t *from_value,
                              mapper_signal_value_t *to_value)
{
    int changed = 0;
    float f = 0;
    
    if (connection->props.muted)
        return 0;

    /* If the destination type is unknown, we can't do anything
     * intelligent here -- even bypass mode might screw up if we
     * assume the types work out. */
    if (connection->props.dest_type != 'f'
        && connection->props.dest_type != 'i')
    {
        return 0;
    }

    if (!connection->props.mode || connection->props.mode == MO_BYPASS)
    {
        if (connection->props.src_type == connection->props.dest_type)
            *to_value = *from_value;
        else if (connection->props.src_type == 'f'
                 && connection->props.dest_type == 'i')
            to_value->i32 = (int)from_value->f;
        else if (connection->props.src_type == 'i'
                 && connection->props.dest_type == 'f')
            to_value->f = (float)from_value->i32;
    }
    else if (connection->props.mode == MO_EXPRESSION
             || connection->props.mode == MO_LINEAR)
    {
        die_unless(connection->expr!=0, "Missing expression.\n");
        *to_value = mapper_expr_evaluate(connection->expr, from_value);
    }

    else if (connection->props.mode == MO_CALIBRATE)
    {
        if (connection->props.src_type == 'f')
            f = from_value->f;
        else if (connection->props.src_type == 'i')
            f = (float)from_value->i32;

        /* If calibration mode has just taken effect, first data
         * sample sets source min and max */
        if (!connection->calibrating) {
            connection->props.range.src_min = f;
            connection->props.range.src_max = f;
            connection->props.range.known |=
                CONNECTION_RANGE_SRC_MIN | CONNECTION_RANGE_SRC_MAX;
            connection->calibrating = 1;
            changed = 1;
        } else {
            if (f < connection->props.range.src_min) {
                connection->props.range.src_min = f;
                connection->props.range.known |= CONNECTION_RANGE_SRC_MIN;
                changed = 1;
            }
            if (f > connection->props.range.src_max) {
                connection->props.range.src_max = f;
                connection->props.range.known |= CONNECTION_RANGE_SRC_MAX;
                changed = 1;
            }
        }

        if (changed) {
            mapper_connection_set_linear_range(connection, sig,
                                               &connection->props.range);

            /* Stay in calibrate mode. */
            connection->props.mode = MO_CALIBRATE;
        }

        if (connection->expr)
            *to_value = mapper_expr_evaluate(connection->expr, from_value);
    }

    return 1;
}
예제 #29
0
bool test_block_cache()
{
    using value_type = std::pair<int, int>;

    constexpr size_t magic1 = 0xc01ddead;

    constexpr unsigned subblock_raw_size = 1024 * 8; // 8KB subblocks
    constexpr unsigned block_size = 128;             // 1MB blocks (=128 subblocks)

    constexpr unsigned num_blocks = 64;              // number of blocks to use for this test
    constexpr unsigned cache_size = 8;               // size of cache in blocks

    using subblock_type = foxxll::typed_block<subblock_raw_size, value_type>;
    using block_type = foxxll::typed_block<block_size* sizeof(subblock_type), subblock_type>;

    constexpr unsigned subblock_size = subblock_type::size;          // size in values

    using bid_type = block_type::bid_type;
    using bid_container_type = std::vector<bid_type>;

    // prepare test: allocate blocks, fill them with values and write to disk
    bid_container_type bids(num_blocks);
    foxxll::block_manager* bm = foxxll::block_manager::get_instance();
    bm->new_blocks(foxxll::striping(), bids.begin(), bids.end());

    block_type* block = new block_type;
    for (unsigned i_block = 0; i_block < num_blocks; i_block++) {
        for (unsigned i_subblock = 0; i_subblock < block_size; i_subblock++) {
            for (unsigned i_value = 0; i_value < subblock_size; i_value++) {
                int value = i_value + i_subblock * subblock_size + i_block * block_size;
                (*block)[i_subblock][i_value] = value_type(value, value);
            }
        }
        foxxll::request_ptr req = block->write(bids[i_block]);
        req->wait();
    }

    std::mt19937 randgen;
    std::uniform_int_distribution<int> distr_num(0, num_blocks - 1);
    std::uniform_int_distribution<int> distr_size(0, block_size - 1);

    // create block_cache
    using cache_type = stxxl::hash_map::block_cache<block_type>;
    cache_type cache(cache_size);

    // load random subblocks and check for values
    int n_runs = cache_size * 10;
    for (int i_run = 0; i_run < n_runs; i_run++) {
        int i_block = distr_num(randgen);
        int i_subblock = distr_size(randgen);

        subblock_type* subblock = cache.get_subblock(bids[i_block], i_subblock);

        int expected = i_block * block_size + i_subblock * subblock_size + 1;
        die_unless((*subblock)[1].first == expected);
    }

    // do the same again but this time with prefetching
    for (int i_run = 0; i_run < n_runs; i_run++) {
        int i_block = distr_num(randgen);
        int i_subblock = distr_size(randgen);

        cache.prefetch_block(bids[i_block]);
        subblock_type* subblock = cache.get_subblock(bids[i_block], i_subblock);
        int expected = i_block * block_size + i_subblock * subblock_size + 1;
        die_unless((*subblock)[1].first == expected);
    }

    // load and modify some subblocks; flush cache and check values
    randgen.seed(magic1);
    for (int i_run = 0; i_run < n_runs; i_run++) {
        int i_block = distr_num(randgen);
        int i_subblock = distr_size(randgen);

        subblock_type* subblock = cache.get_subblock(bids[i_block], i_subblock);

        die_unless(cache.make_dirty(bids[i_block]));
        (*subblock)[1].first = (*subblock)[1].second + 42;
    }

    randgen.seed(magic1);
    for (int i_run = 0; i_run < n_runs; i_run++) {
        int i_block = distr_num(randgen);
        int i_subblock = distr_size(randgen);
        subblock_type* subblock = cache.get_subblock(bids[i_block], i_subblock);

        int expected = i_block * block_size + i_subblock * subblock_size + 1;
        die_unequal((*subblock)[1].first, expected + 42);
    }

    // test retaining
    cache.clear();

    // not yet cached
    die_unless(cache.retain_block(bids[0]) == false);
    cache.prefetch_block(bids[0]);

    // cached, should be retained
    die_unless(cache.retain_block(bids[0]) == true);
    // release again
    die_unless(cache.release_block(bids[0]) == true);
    // retrain-count should be 0, release fails
    die_unless(cache.release_block(bids[0]) == false);

    // cache new block
    subblock_type* kicked_subblock = cache.get_subblock(bids[1], 0);
    // load other blocks, so that kicked_subblock, well, gets kicked
    for (unsigned i = 0; i < cache_size + 5; i++) {
        cache.prefetch_block(bids[i + 3]);
    }
    // load kicked subblock again, should be at a different location
    die_unless(cache.get_subblock(bids[1], 0) != kicked_subblock);

    subblock_type* retained_subblock = cache.get_subblock(bids[1], 0);
    // now retain subblock
    die_unless(cache.retain_block(bids[1]) == true);
    for (unsigned i = 0; i < cache_size + 5; i++) {
        cache.prefetch_block(bids[i + 3]);
    }
    // retained_subblock should not have been kicked
    die_unless(cache.get_subblock(bids[1], 0) == retained_subblock);
    cache.clear();

    // test swapping
    subblock_type* a_subblock = cache.get_subblock(bids[6], 1);
    cache_type cache2(cache_size / 2);
    std::swap(cache, cache2);
    die_unless(cache.size() == cache_size / 2);
    die_unless(cache2.size() == cache_size);
    die_unless(cache2.get_subblock(bids[6], 1) == a_subblock);
    delete block;

    LOG1 << "Passed Block-Cache Test";

    return true;
}
예제 #30
0
static int ejabberd_zlib_drv_control(ErlDrvData handle,
				     unsigned int command,
				     char *buf, int len,
				     char **rbuf, int rlen)
{
   ejabberd_zlib_data *d = (ejabberd_zlib_data *)handle;
   int err;
   int size;
   ErlDrvBinary *b;

   switch (command)
   {
      case DEFLATE:
	 size = BUF_SIZE + 1;
	 rlen = 1;
	 b = driver_alloc_binary(size);
	 b->orig_bytes[0] = 0;

	 d->d_stream->next_in = (unsigned char *)buf;
	 d->d_stream->avail_in = len;
	 d->d_stream->avail_out = 0;
	 err = Z_OK;

	 while (err == Z_OK && d->d_stream->avail_out == 0)
	 {
	    d->d_stream->next_out = (unsigned char *)b->orig_bytes + rlen;
	    d->d_stream->avail_out = BUF_SIZE;

	    err = deflate(d->d_stream, Z_SYNC_FLUSH);
	    die_unless((err == Z_OK) || (err == Z_STREAM_END),
		       "Deflate error");

	    rlen += (BUF_SIZE - d->d_stream->avail_out);
	    size += (BUF_SIZE - d->d_stream->avail_out);
	    b = driver_realloc_binary(b, size);
	 }
	 b = driver_realloc_binary(b, rlen);
	 *rbuf = (char *)b;
	 return rlen;
      case INFLATE:
	 size = BUF_SIZE + 1;
	 rlen = 1;
	 b = driver_alloc_binary(size);
	 b->orig_bytes[0] = 0;

	 if (len > 0) {
	    d->i_stream->next_in = (unsigned char *)buf;
	    d->i_stream->avail_in = len;
	    d->i_stream->avail_out = 0;
	    err = Z_OK;

	    while (err == Z_OK && d->i_stream->avail_out == 0)
	    {
	       d->i_stream->next_out = (unsigned char *)b->orig_bytes + rlen;
	       d->i_stream->avail_out = BUF_SIZE;

	       err = inflate(d->i_stream, Z_SYNC_FLUSH);
	       die_unless((err == Z_OK) || (err == Z_STREAM_END),
			  "Inflate error");

	       rlen += (BUF_SIZE - d->i_stream->avail_out);
	       size += (BUF_SIZE - d->i_stream->avail_out);
	       b = driver_realloc_binary(b, size);
	    }
	 }
	 b = driver_realloc_binary(b, rlen);
	 *rbuf = (char *)b;
	 return rlen;
   }

   b = driver_alloc_binary(1);
   b->orig_bytes[0] = 0;
   *rbuf = (char *)b;
   return 1;
}