Hi,
i'm trying to use a binary that has a main node, and every leaf is also a
node that has leaves.
So i'm using a struct that's called node and this node has two nodes in it.
i defined a struct like

typedef struct element{
    int IP;
    int counter;
}ELEMENT;

typedef struct node {
    ELEMENT node_element;
    struct node *left;
    struct node *right;
}NODE;

(i attached my code to the mail)

i want to add a new node into tree and i use shared memory to hold the
entire tree. If the IP of new node is smaller, i put it to he left leaf. If
IP of new node is greater, i put it into the right leaf. The right and left
leaves are also nodes and they have right and left leaves.

Anyway, my point is when i want to insert new nodes, i'm using the
apr_shm_baseaddr_get function to get the address and try to insert ner node
into it. But when i add another node as a leaf, it also changes the root
node.

the examle output of the attached code is like this (i'm printing the root
node and the right leaf node)
193168221
193168221
they are same.
what i'm expecting is
192168221
193168221

Do you have any advices?

Oğuzhan TOPGÜL
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <apr_shm.h>
#include <apr_global_mutex.h>
#include "util_mutex.h"
#include "http_log.h"  //APLOG_MARK, APLOG_ERR
#include "apr.h"
#include "apr_strings.h"
#include "http_core.h"
#include "ap_config.h"

//NODE and ELEMENT struct definitions
typedef struct element{
    int IP;
    int counter;
}ELEMENT;

typedef struct node {
    ELEMENT node_element; 
    struct node *left; 
    struct node *right; 
}NODE;

//Shared memory and global variable definitions
apr_shm_t *my_shm;  /* Pointer to shared memory block */ /* Shared memory structure */

struct node *newNode(int IP, struct node *node, apr_shm_t *shm) {
  node=(NODE*)apr_shm_baseaddr_get(shm);
  node->node_element.IP=IP;
  node->node_element.counter=1;
  node->left = NULL; 
  node->right = NULL;
  return(node); 
} 

struct node *insert(struct node *tree, int IP, apr_shm_t *shm) {
  // 1. If the tree is empty, return a new, single node 
  if (tree == NULL){
  return(newNode(IP, tree, shm));
  }
  
  else 
  { 
    // 2. Otherwise, recur down the tree 
    if (IP < tree->node_element.IP) 
        tree->left = insert(tree->left, IP, shm); 
    else 
        
        if (IP > tree->node_element.IP) 
             tree->right = insert(tree->right, IP, shm);
        else
        {     
            if (tree->node_element.IP==IP)
               return NULL;
         }         
    return(tree); // return the (unchanged) node pointer 
  } 
}

///////////////////////Binary Tree ve Fonksiyonlar Bitti///////////////////

static int my_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s){
    apr_status_t rs;
    
    //Shared Memory segment
    rs = apr_shm_create(&my_shm, sizeof(NODE), NULL, pconf);
       
    
    apr_pool_cleanup_register(pconf, NULL, shm_cleanup_wrapper, apr_pool_cleanup_null);
    return OK;
}

static int my_handler(request_rec *r)
{
   apr_status_t rs;
   
   if (strcmp(r->handler, "myhandler")) {
        return DECLINED;
    }

   
   binarytree=(NODE*)apr_shm_baseaddr_get(my_shm);
   binarytree=insert(binarytree,19216822,my_shm);
   binarytree=insert(binarytree,19316822,my_shm);

   ap_rprintf(r, "%i%i\n", binarytree->node_element.IP, binarytree->node_element.counter);
   ap_rprintf(r, "%i%i\n", binarytree->right->node_element.IP, binarytree->right->node_element.counter);
   
return OK;
}

static void my_hooks(apr_pool_t *pool)
{
    ap_hook_pre_config(my_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_post_config(my_post_config, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_child_init(my_child_init, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_handler(my_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA my_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
my_hooks
};

Reply via email to