From patchwork Mon Jan 25 16:26:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suraev X-Patchwork-Id: 572800 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.osmocom.org (lists.osmocom.org [IPv6:2a01:4f8:191:444b::2:7]) by ozlabs.org (Postfix) with ESMTP id 0078814032B for ; Tue, 26 Jan 2016 03:26:59 +1100 (AEDT) Received: from lists.osmocom.org (lists.osmocom.org [144.76.43.76]) by lists.osmocom.org (Postfix) with ESMTP id 714CBBA77; Mon, 25 Jan 2016 16:26:57 +0000 (UTC) X-Original-To: openbsc@lists.osmocom.org Delivered-To: openbsc@lists.osmocom.org Received: from hylle06.itea.ntnu.no (hylle06.itea.ntnu.no [129.241.56.235]) by lists.osmocom.org (Postfix) with ESMTP id 78E7FBA5F for ; Mon, 25 Jan 2016 16:26:55 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hylle06.itea.ntnu.no (Postfix) with ESMTP id A490A66287B; Mon, 25 Jan 2016 17:26:55 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at hylle06.itea.ntnu.no Received: from alumnimail01.it.ntnu.no (alumnimail01.it.ntnu.no [129.241.18.54]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hylle06.itea.ntnu.no (Postfix) with ESMTPS id CACB0662812; Mon, 25 Jan 2016 17:26:54 +0100 (CET) Received: from [10.9.1.181] (ip5b418565.dynamic.kabel-deutschland.de [91.65.133.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: suraev) by alumnimail01.it.ntnu.no (Postfix) with ESMTPSA id 821E163FA6; Mon, 25 Jan 2016 17:26:54 +0100 (CET) Subject: Re: [PATCH 3/5] Add bitvector functions from osmo-pcu To: Holger Freyther References: <1453477619-27336-1-git-send-email-suraev@alumni.ntnu.no> <1453477619-27336-3-git-send-email-suraev@alumni.ntnu.no> <656A0B94-6056-49AD-A8A3-74434E5195C4@freyther.de> <56A614F4.3000903@alumni.ntnu.no> <68858672-9913-4510-A275-245C4891485F@freyther.de> From: =?UTF-8?B?TWF4ICjimK0p?= Message-ID: <56A64CCD.8090403@alumni.ntnu.no> Date: Mon, 25 Jan 2016 17:26:53 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <68858672-9913-4510-A275-245C4891485F@freyther.de> X-BeenThere: openbsc@lists.osmocom.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Development of the OpenBSC GSM base station controller List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: openbsc@lists.osmocom.org Errors-To: openbsc-bounces@lists.osmocom.org Sender: "OpenBSC" v2 attached - added hierarchical allocation. cheers, Max. From bf86d3a8449721968d3016793ee34420fc128492 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 25 Jan 2016 17:24:18 +0100 Subject: [PATCH] Add bitvec-related functions from Osmo-PCU Allocation, pack/unpack, field access and helper routines used extensively by Osmo-PCU. Whenever memory allocation happens, alocator context is passed explicitly by caller. --- include/osmocom/core/bitvec.h | 9 ++++ src/bitvec.c | 99 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h index 2415a81..d64d69d 100644 --- a/include/osmocom/core/bitvec.h +++ b/include/osmocom/core/bitvec.h @@ -3,6 +3,7 @@ /* bit vector utility routines */ /* (C) 2009 by Harald Welte + * (C) 2012 Ivan Klyuchnikov * * All Rights Reserved * @@ -39,6 +40,7 @@ */ #include +#include /*! \brief A single GSM bit * @@ -73,5 +75,12 @@ int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit); int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count); int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count); +struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *bvctx); +void bitvec_free(struct bitvec *bv); +int bitvec_unhex(struct bitvec *bv, const char *src); +unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer); +unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer); +uint64_t bitvec_read_field(struct bitvec *bv, unsigned int read_index, unsigned int len); +int bitvec_write_field(struct bitvec *bv, unsigned int write_index, uint64_t val, unsigned int len); /*! @} */ diff --git a/src/bitvec.c b/src/bitvec.c index 8596d51..96f8ea6 100644 --- a/src/bitvec.c +++ b/src/bitvec.c @@ -1,6 +1,7 @@ /* bit vector utility routines */ /* (C) 2009 by Harald Welte + * (C) 2012 Ivan Klyuchnikov * (C) 2015 by Sysmocom s.f.m.c. GmbH * * All Rights Reserved @@ -32,6 +33,8 @@ #include #include #include +#include +#include #include @@ -336,4 +339,100 @@ int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count bv->cur_bit += count * 8; return 0; } + +struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *ctx) +{ + TALLOC_CTX *bvctx = talloc_zero(ctx, struct bitvec); + if (!bvctx) + return NULL; + + struct bitvec *bv = bvctx; + + bv->data = talloc_zero_array(bvctx, uint8_t, size); + if (!(bv->data)) { + talloc_free(bv); + return NULL; + } + + bv->data_len = size; + bv->cur_bit = 0; + return bv; +} + +void bitvec_free(struct bitvec *bv) +{ + talloc_free(bv->data); + talloc_free(bv); +} + +unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer) +{ + unsigned int i = 0; + for (i = 0; i < bv->data_len; i++) + { + buffer[i] = bv->data[i]; + } + return i; +} + +unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer) +{ + unsigned int i = 0; + for (i = 0; i < bv->data_len; i++) { + bv->data[i] = buffer[i]; + } + return i; +} + + +int bitvec_unhex(struct bitvec *bv, const char *src) +{ + unsigned val; + unsigned write_index = 0; + unsigned digits = bv->data_len*2; + for (unsigned i=0; icur_bit = read_index; + + for (i = 0; i < len; i++) { + int bit = bitvec_get_bit_pos((const struct bitvec *)bv, bv->cur_bit); + if (bit < 0) + return bit; + if (bit) + ui |= ((uint64_t)1 << (len - i - 1)); + bv->cur_bit++; + } + read_index += len; + return ui; +} + + +int bitvec_write_field(struct bitvec *bv, unsigned int write_index, uint64_t val, unsigned int len) +{ + unsigned int i; + int rc; + bv->cur_bit = write_index; + for (i = 0; i < len; i++) { + int bit = 0; + if (val & ((uint64_t)1 << (len - i - 1))) + bit = 1; + rc = bitvec_set_bit(bv, bit); + if (rc) + return rc; + } + write_index += len; + return 0; +} + /*! @} */ -- 2.5.0