From: Enrico Scholz Date: Thu, 17 Mar 2005 14:51:55 +0000 (+0000) Subject: initial checkin X-Git-Tag: IPSENTINEL_VERSION_0_12~98 X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e18c527d4ff7435c758f7bb4c88c936aa1c8024;p=util-vserver.git initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@1896 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- diff --git a/util-vserver/ensc_vector/list-add.c b/util-vserver/ensc_vector/list-add.c new file mode 100644 index 0000000..b7aea32 --- /dev/null +++ b/util-vserver/ensc_vector/list-add.c @@ -0,0 +1,31 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "list.h" +#include "list-internal.h" + + +void * +List_add(struct List *list, void const *data) +{ + return List_insertInternal(list, data, &list->root, 0)->data; +} diff --git a/util-vserver/ensc_vector/list-at.c b/util-vserver/ensc_vector/list-at.c new file mode 100644 index 0000000..22f5aa1 --- /dev/null +++ b/util-vserver/ensc_vector/list-at.c @@ -0,0 +1,36 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "list.h" +#include "list-internal.h" + +void const * +List_at_const(struct List const *l, size_t idx) +{ + struct ListItem const *itm = l->root; + + for (; itm!=0 && idx>0; --idx) + itm = itm->next; + + if (itm!=0) return itm->data; + else return 0; +} diff --git a/util-vserver/ensc_vector/list-free.c b/util-vserver/ensc_vector/list-free.c new file mode 100644 index 0000000..da68a24 --- /dev/null +++ b/util-vserver/ensc_vector/list-free.c @@ -0,0 +1,42 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "list.h" +#include "list-internal.h" + +void +List_free(struct List *list) +{ + for (struct ListItem *itm = list->root; + itm!=0;) + { + struct ListItem *next = itm->next; + + free(itm->data); +#ifndef NDEBUG + itm->data = (void *)(0xdeadbeaf); +#endif + free(itm); + + itm = next; + } +} diff --git a/util-vserver/ensc_vector/list-init.c b/util-vserver/ensc_vector/list-init.c new file mode 100644 index 0000000..8bcc63e --- /dev/null +++ b/util-vserver/ensc_vector/list-init.c @@ -0,0 +1,31 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "list.h" + +void +List_init(struct List *list, size_t elem_size) +{ + list->root = 0; + list->count = 0; + list->elem_size = elem_size; +} diff --git a/util-vserver/ensc_vector/list-insertinternal.c b/util-vserver/ensc_vector/list-insertinternal.c new file mode 100644 index 0000000..8d301ca --- /dev/null +++ b/util-vserver/ensc_vector/list-insertinternal.c @@ -0,0 +1,57 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "list.h" +#include "list-internal.h" + +#include +#include + +#define ENSC_WRAPPERS_STDLIB 1 +#include + +struct ListItem * +List_insertInternal(struct List *list, void const *data, + struct ListItem **before_pos, + struct ListItem *after_pos) +{ + struct ListItem *item = Emalloc(sizeof(struct ListItem)); + + assert((before_pos!=0 || after_pos!=0) && + (before_pos==0 || after_pos==0)); + + item->data = Emalloc(list->elem_size); + memcpy(item->data, data, list->elem_size); + + if (before_pos!=0) { + item->next = *before_pos; + *before_pos = item; + } + else { + item->next = after_pos->next; + after_pos->next = item; + } + + ++list->count; + + return item; +} diff --git a/util-vserver/ensc_vector/list-internal.h b/util-vserver/ensc_vector/list-internal.h new file mode 100644 index 0000000..95c09e8 --- /dev/null +++ b/util-vserver/ensc_vector/list-internal.h @@ -0,0 +1,33 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifndef H_UTILVSERVER_VECTOR_LIST_INTERNAL_H +#define H_UTILVSERVER_VECTOR_LIST_INTERNAL_H + +struct ListItem +{ + void *data; + struct ListItem *next; +}; + +struct ListItem * List_insertInternal(struct List *list, + void const *data, + struct ListItem **before_pos, + struct ListItem *after_pos); + +#endif // H_UTILVSERVER_VECTOR_LIST_INTERNAL_H diff --git a/util-vserver/ensc_vector/list-search.c b/util-vserver/ensc_vector/list-search.c new file mode 100644 index 0000000..4552ca7 --- /dev/null +++ b/util-vserver/ensc_vector/list-search.c @@ -0,0 +1,37 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "list.h" +#include "list-internal.h" + +void const * +List_search(struct List const *list, void const *key, + int (*compare)(const void *, const void *)) +{ + struct ListItem const *itm = list->root; + + while (itm!=0 && compare(itm->data, key)!=0) + itm = itm->next; + + if (itm!=0) return itm->data; + else return 0; +} diff --git a/util-vserver/ensc_vector/list-searchselforg.c b/util-vserver/ensc_vector/list-searchselforg.c new file mode 100644 index 0000000..24e855f --- /dev/null +++ b/util-vserver/ensc_vector/list-searchselforg.c @@ -0,0 +1,76 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "list.h" +#include "list-internal.h" + +#include +#include + +void const * +List_searchSelfOrg(struct List const *list, void const *key, + int (*compare)(const void *, const void *), + ListSelfOrgMethod method) +{ + struct List *list_v = (struct List *)(list); + struct ListItem **itm = &list_v->root; + + switch (method) { + case listMOVE_FRONT : + while (*itm!=0 && compare((*itm)->data, key)!=0) + itm = &(*itm)->next; + + if (*itm && *itm!=list->root) { + struct ListItem *res = *itm; + + *itm = res->next; + res->next = list->root; + list_v->root = res; + + itm = &list_v->root; + } + break; + + case listSHIFT_ONCE : + if (*itm!=0 && compare((*itm)->data, key)!=0) { + while ((*itm)->next!=0 && + compare((*itm)->next->data, key)!=0) + itm = &(*itm)->next; + + if ((*itm)->next==0) + itm = &(*itm)->next; + else { + struct ListItem *res = (*itm)->next; + + (*itm)->next = res->next; + res->next = *itm; + *itm = res; + } + } + break; + + default : assert(false); return 0; + } + + if (*itm!=0) return (*itm)->data; + else return 0; +} diff --git a/util-vserver/ensc_vector/list.h b/util-vserver/ensc_vector/list.h new file mode 100644 index 0000000..2fab00f --- /dev/null +++ b/util-vserver/ensc_vector/list.h @@ -0,0 +1,47 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifndef H_UTILVSERVER_VECTOR_LIST_H +#define H_UTILVSERVER_VECTOR_LIST_H + +#include + +struct ListItem; +struct List +{ + struct ListItem *root; + size_t count; + size_t elem_size; +}; + +typedef enum { listMOVE_FRONT, listSHIFT_ONCE } ListSelfOrgMethod; + +void List_init(struct List *, size_t elem_size); +void List_free(struct List *); +void * List_add(struct List *, void const *key); +void * List_at(struct List *, size_t idx); +void const * List_at_const(struct List const *, size_t idx); + +void const * List_search(struct List const *, void const *key, + int (*compare)(const void *, const void *)); + +void const * List_searchSelfOrg(struct List const *, void const *key, + int (*compare)(const void *, const void *), + ListSelfOrgMethod method); + +#endif // H_UTILVSERVER_VECTOR_LIST_H diff --git a/util-vserver/ensc_vector/list.hc b/util-vserver/ensc_vector/list.hc new file mode 100644 index 0000000..62eede6 --- /dev/null +++ b/util-vserver/ensc_vector/list.hc @@ -0,0 +1,23 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +static inline UNUSED void * +List_at(struct List *list, size_t idx) +{ + return (void *)(List_at_const(list, idx)); +} diff --git a/util-vserver/ensc_vector/testsuite/test2.c b/util-vserver/ensc_vector/testsuite/test2.c new file mode 100644 index 0000000..4cb3097 --- /dev/null +++ b/util-vserver/ensc_vector/testsuite/test2.c @@ -0,0 +1,142 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "ensc_vector/list.h" +#include "ensc_vector/list-internal.h" + +#include +#include +#include + +int wrapper_exit_code = 2; + +static int +cmp(void const *lhs_v, void const *rhs_v) +{ + int const * const lhs = lhs_v; + int const * const rhs = rhs_v; + + return *lhs - *rhs; +} + +struct List l; + + +static void A(int val) +{ + int * res = List_add(&l, &val); + + assert(*res == val); +} + +static int const * S(int val) +{ + return List_search(&l, &val, cmp); +} + +static int const * SSO_F(int val) +{ + return List_searchSelfOrg(&l, &val, cmp, listMOVE_FRONT); +} + +static int const * SSO_S(int val) +{ + return List_searchSelfOrg(&l, &val, cmp, listSHIFT_ONCE); +} + +static int P(size_t idx) +{ + int const *res = List_at_const(&l, idx); + + assert(res!=0); + return *res; +} + +static bool P0(size_t idx) +{ + return List_at_const(&l, idx) == 0; +} + +static bool CMP(int const *lhs, int rhs) +{ + return (lhs!=0 && *lhs==rhs) || (lhs==0 && rhs==-1); +} + + +int main() +{ + List_init(&l, sizeof(int)); + + A(5); A(4); A(3); A(2); A(1); A(0); + assert(P(0)==0 && P(1)==1 && P(2)==2 && P(3)==3 && P(4)==4 && P(5)==5 && P0(6)); + + assert(CMP(S(5), 5) && CMP(S(2), 2) && CMP(S(0), 0)); + assert(CMP(S(42),-1)); + + assert(CMP(SSO_F(5), 5)); + assert(P(0)==5 && P(1)==0 && P(2)==1 && P(3)==2 && P(4)==3 && P(5)==4 && P0(6)); + + assert(CMP(SSO_F(5), 5)); + assert(P(0)==5 && P(1)==0 && P(2)==1 && P(3)==2 && P(4)==3 && P(5)==4 && P0(6)); + + assert(CMP(SSO_F(0), 0)); + assert(P(0)==0 && P(1)==5 && P(2)==1 && P(3)==2 && P(4)==3 && P(5)==4 && P0(6)); + + assert(CMP(SSO_F(4), 4)); + assert(P(0)==4 && P(1)==0 && P(2)==5 && P(3)==1 && P(4)==2 && P(5)==3 && P0(6)); + + assert(CMP(SSO_F(5), 5)); + assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==1 && P(4)==2 && P(5)==3 && P0(6)); + + assert(CMP(SSO_F(42),-1)); + assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==1 && P(4)==2 && P(5)==3 && P0(6)); + + + + assert(CMP(SSO_S(3), 3)); + assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==1 && P(4)==3 && P(5)==2 && P0(6)); + + assert(CMP(SSO_S(3), 3)); + assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6)); + + assert(CMP(SSO_S(5), 5)); + assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6)); + + assert(CMP(SSO_S(4), 4)); + assert(P(0)==4 && P(1)==5 && P(2)==0 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6)); + + assert(CMP(SSO_S(0), 0)); + assert(P(0)==4 && P(1)==0 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6)); + + assert(CMP(SSO_S(0), 0)); + assert(P(0)==0 && P(1)==4 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6)); + + assert(CMP(SSO_S(0), 0)); + assert(P(0)==0 && P(1)==4 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6)); + + assert(CMP(SSO_S(42), -1)); + assert(P(0)==0 && P(1)==4 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6)); + + List_free(&l); + + return EXIT_SUCCESS; +} diff --git a/util-vserver/ensc_vector/vector-searchselforg.c b/util-vserver/ensc_vector/vector-searchselforg.c new file mode 100644 index 0000000..9923ca9 --- /dev/null +++ b/util-vserver/ensc_vector/vector-searchselforg.c @@ -0,0 +1,71 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "vector.h" +#include "vector-internal.h" + +#include +#include +#include + +void * +Vector_searchSelfOrg(struct Vector *vec, void const *key, + int (*compare)(const void *, const void *), + VectorSelfOrgMethod method) +{ + char * const start_ptr = vec->data; + char * const end_ptr = start_ptr + vec->count*vec->elem_size; + char *ptr = start_ptr; + + for (; ptrelem_size; + + if (end_ptr <= ptr) ptr = 0; + else if (start_ptr < ptr) { + char tmp[vec->elem_size]; + memcpy(tmp, ptr, vec->elem_size); + + assert(ptr >= start_ptr+vec->elem_size); + + switch (method) { + case vecMOVE_FRONT : + memmove(start_ptr+vec->elem_size, start_ptr, ptr - start_ptr); + + ptr = start_ptr; + break; + + case vecSHIFT_ONCE : + memmove(ptr, ptr - vec->elem_size, vec->elem_size); + ptr -= vec->elem_size; + break; + + default : + assert(false); + ptr = 0; + } + + memcpy (ptr, tmp, vec->elem_size); + } + + return ptr; +} + diff --git a/util-vserver/lib_internal/unify-isiunlinkable.c b/util-vserver/lib_internal/unify-isiunlinkable.c new file mode 100644 index 0000000..eb76e15 --- /dev/null +++ b/util-vserver/lib_internal/unify-isiunlinkable.c @@ -0,0 +1,38 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 Enrico Scholz +// +// This program 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; version 2 of the License. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "unify.h" +#include "vserver.h" + + +bool +Unify_isIUnlinkable(char const *filename) +{ + uint_least32_t const V = VC_IATTR_IUNLINK|VC_IATTR_IMMUTABLE; + + uint_least32_t flags; + uint_least32_t mask = V; + + return (vc_get_iattr(filename, 0, &flags, &mask)!=-1 && + (mask & V)==V && + (flags & V)!=V); +}