123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /*
- * Copyright (C) 2020 Weber Yann
- *
- * This file is part of pyrpn.
- *
- * pyrpn is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * pyrpn is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "rpn_if.h"
-
- rpn_if_t* rpn_if_new(size_t mem_sz, rpn_if_transfo_t *if2rpn,
- rpn_if_transfo_t *rpn2if, rpn_expr_t *rpn)
- {
- rpn_if_t *res;
- if(mem_sz != 0)
- {
- if((if2rpn->mem_sz != 0 && if2rpn->mem_sz != mem_sz) ||
- (rpn2if->mem_sz != 0 && rpn2if->mem_sz != mem_sz))
- {
- errno = EINVAL;
- return NULL;
- }
- }
- else
- {
- if(if2rpn->mem_sz == 0 && rpn2if->mem_sz == 0)
- {
- errno = EINVAL;
- return NULL;
- }
- if(if2rpn->mem_sz != rpn2if->mem_sz && if2rpn->mem_sz != 0 &&
- rpn2if->mem_sz != 0)
- {
- errno = EINVAL;
- return NULL;
- }
- mem_sz = if2rpn->mem_sz != 0?if2rpn->mem_sz:rpn2if->mem_sz;
- }
- if(if2rpn->data_sz != rpn2if->data_sz || if2rpn->data_sz == 0)
- {
- errno = EINVAL;
- return NULL;
- }
- res = _rpn_if_new(mem_sz, if2rpn->argc, rpn2if->argc, rpn2if->data_sz);
- if(!res)
- {
- return NULL;
- }
- res->if2rpn = if2rpn->if2rpn;
- res->rpn2if = rpn2if->rpn2if;
- res->rpn = rpn;
- return res;
- }
-
- void rpn_if_free(rpn_if_t* rif)
- {
- size_t i;
- for(i=0; i<rif->rpn_sz; i++)
- {
- rpn_expr_close(&(rif->rpn[i]));
- }
- free(rif->rpn);
- free(rif->rpn_res);
- munmap(rif->mem, rif->mem_sz);
- free(rif);
- }
-
- int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
- {
- return rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
- }
-
- int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
- size_t offset)
- {
- size_t i;
- for(i=offset; i<offset+sz; i++)
- {
- /**@todo continue implementation */
- }
- return 0;
- }
-
- rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count, size_t
- value_sz)
- {
- rpn_if_t *res;
- int err;
-
- res = malloc(sizeof(rpn_if_t));
- if(!res)
- {
- goto error;
- }
-
- res->rpn_sz = rpn_count;
- res->rpn_argc = rpn_argc;
- res->mem_sz = mem_sz;
- res->value_sz = value_sz;
-
- res->mem = mmap(NULL, mem_sz, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
- if(res->mem == (void*)-1)
- {
- goto mmap_err;
- }
-
- res->rpn_res = malloc(sizeof(unsigned long) * (rpn_count +rpn_argc));
- if(!res->rpn_res)
- {
- goto rpn_malloc_err;
- }
- res->rpn_args = &(res->rpn_res[res->rpn_sz]);
-
- res->rpn = malloc(sizeof(rpn_expr_t) * res->rpn_sz);
- if(!res->rpn)
- {
- goto rpn_expr_err;
- }
-
- return res;
-
- rpn_expr_err:
- err = errno;
- free(res->rpn_res);
- rpn_malloc_err:
- err = errno;
- munmap(res->mem, mem_sz);
- mmap_err:
- err = errno;
- free(res);
- error:
- err = errno;
- errno = err;
- return NULL;
- }
|