Am I getting closer? Now to set maxmail current mail, etc use a long
int. if the setting is password or encoding type it takes a string.
Those are just functions for setting single values. Then I have the
setmulti. It needs all values to be converted to strings before hand
is there any pseudo variable types in C? where I can pass a int,
char,long or double to a function then find out what it is?  I can see
having the single gets and stores in some places but then when a user
is first authenicated it grabs alot of data that can be cached right
at login. I don't want to do 5 tcp connections for every value. I want
1 tcp connection pushing all the data then closing the connection. Or
can I turn the memcache instance into a global for a persistent
connection? Or that boils down to threading. would need a separate
thread initiated. Hmm. 

char *memcache_gstr(char *key, int id){
        char *ret;
        char *keyname;
        char keyid[4];
        ret = g_new0(char,VALUE_SIZE);
        keyname = g_new0(char,KEY_SIZE);
        memset(keyid, 0, sizeof(keyid));

        sprintf(keyid,"%d",id);
        keyname= construct_keyname(key,keyid);
        printf("key: %sn", keyname);
        ret = getmc(keyname);
        free(keyname);
        return ret;
}

char *memcache_charreturn(long int *number){
        char *buffer;
        buffer = g_new0(char,VALUE_SIZE);
        snprintf(buffer,"%d",number);
        return buffer;
}

char *memcache_gencode(int id){
        return memcache_gstr("encode",id);
}
long int memcache_gmaxmail(int id){
        return strtol(memcache_gstr("maxmail",id),NULL,0);
}

long int memcache_gcurmail(int id){
        return strtol(memcache_gstr("curmail",id),NULL,0);
}

long int *memcache_gmaxseive(int id){
        return strtol(memcache_gstr("maxseive",id),NULL,0);
}

void memcache_smaxmail(int id, long int *value){
        memcache_sstr("maxmail",id,memcache_charreturn(value));
}

void memcache_sencode(int id,char *value){
        memcache_sstr("encode",id,value);
}
void memcache_scurmail(int id, long int *value){
        memcache_sstr("curmail",id,memcache_charreturn(value));
}
void memcache_sseive(int id, long int *value){
        memcache_sstr("maxseive",id,memcache_charreturn(value));
}

char* memcache_gpasswd(int id){
        return memcache_gstr("passwd",id);
}

void memcache_spasswd(int id,char *value){
        memcache_sstr("passwd",id,value);
}
On 04/01/2012 at 6:08 PM, skr...@hushmail.com wrote:
>>         sprintf(keyid,"%d",id);

>snprintf(keyid, 4, "%d", id);

>but 4 seems rather smallish. Also, why map ints to chars here? Might
as
>well defer that to the snprintf call in construct_keyname, changing
the
>'postfix' argument to an int.
I change it to a char because of mapping idnr numbers to usernames,
and also libmemcache takes strings for keynames.
On 04/01/2012 at 1:56 PM, Paul J Stevens  wrote:On 04/01/2012 02:05
PM, skr...@hushmail.com wrote:
On 04/01/2012 02:05 PM, skr...@hushmail.com wrote:
> 
> Ok say I have this
> 
> char* construct_keyname(char *key, char *postfix){
>         char *keyname;
>         keyname = (char *)malloc(KEY_SIZE*sizeof(char)); //allocate
> memory that is freed inmemcache_gstr and memcache_gid
> 
>         snprintf(keyname,
>                 (
>                        
(strlen(MEMCACHE_PRFX)+strlen(postfix)+strlen(key))
>                         +3
>                 ),
>                 "%s%s_%s",
>                 MEMCACHE_PRFX,
>                 key,
>                 postfix);
> 
>         return keyname;
> }
> 
> I have allocated memory for the prefix name
> 
> char *memcache_gstr(char *key, int id){
>         char *ret;
>         char *keyname;
>         char keyid[4];
>         sprintf(keyid,"%d",id);
>         keyname= construct_keyname(key,keyid);
>         printf("key: %sn", keyname);
>         ret = getmc(keyname);
>         free(keyname);
>         return ret;
> }
> then I call it here and since the return value of the keyname
> constructor is allocated memory can I free it with the pointer that
I
> assigned to it? ie: keyname
> 
> keyname is a pointer that points to the return value of
> construct_keyname() and the return value is allocated on the "heap".
Is
> that right?
> 
> 
> On 04/01/2012 at 7:51 AM, Paul J Stevens  wrote:
> 
>     On 04/01/2012 02:38 AM, skr...@hushmail.com wrote:
>     > https://github.com/skrapsrwt/dbmail-memcache/
> 
>     I looked at the 'push change and added some comments.
> 
>     Most importantly you need to understand C doesn't have automatic
>     allocation/de-allocation of memory like PHP does.
> 
>     If you want to assign something to a chunk of memory that memory
must be
>     allocated, either on the 'stack', or on the 'heap'.
> 
>     The stack is local to the scope. So it can be passed
'down-stream' if
>     you are careful, but it can never be returned to the calling
scope.
> 
>     The heap is where malloc/free do their thing: a global memory
space that
>     you can use - if you use it following the rules. Heap memory is
always
>     safe to pass around.
> 
>     - you must allocated everything you need.
>     - for each malloc call, there must be a free call.
> 
>     stack allocation:
> 
>     char foo[128]; // char array on the stack you can assign 128
bytes to
> 
>     char *foo = g_new0(char, 128); // same size char array on the
heap
> 
> 
>     g_new0 is a simple wrapper around calloc. It allocates memory
and cleans
>     it out.
> 
>     If you allocate on the stack it is always a good idea to clean
it
>     explicitely:
> 
>     char foo[128];
>     memset(foo, 0, sizeof(foo));
> 
> 
> 
>     -- 
>     ________________________________________________________________
>     Paul J Stevens pjstevns @ gmail, twitter, skype, linkedin
> 
>     * Premium Hosting Services and Web Application Consultancy *
> 
>     www.nfg.nl/i...@nfg.nl/+31.85.877.99.97
>     ;
>     ________________________________________________________________
>     _______________________________________________
>     Dbmail-dev mailing list
>     Dbmail-dev@dbmail.org
>     http://mailman.fastxs.nl/cgi-bin/mailman/listinfo/dbmail-dev
>     ;
> 
> 
> 
> _______________________________________________
> Dbmail-dev mailing list
> Dbmail-dev@dbmail.org
> http://mailman.fastxs.nl/cgi-bin/mailman/listinfo/dbmail-dev
-- 
________________________________________________________________
Paul J Stevens        pjstevns @ gmail, twitter, skype, linkedin

  * Premium Hosting Services and Web Application Consultancy *

           www.nfg.nl/i...@nfg.nl/+31.85.877.99.97
________________________________________________________________
> 
> Ok say I have this
> 
> char* construct_keyname(char *key, char *postfix){
>         char *keyname;
>         keyname = (char *)malloc(KEY_SIZE*sizeof(char)); //allocate
> memory that is freed inmemcache_gstr and memcache_gid

Use:

keyname = g_new0(char, KEY_SIZE);

which will clean out the assigned memory segment.
> 
>         snprintf(keyname,
>                 (
>                        
(strlen(MEMCACHE_PRFX)+strlen(postfix)+strlen(key))
>                         +3
>                 ),

This ain't right.

the second argument to snprintf doesn't have to be the exact size of
the
remainder arguments. Just the max size of the string in the first
argument. So:

snprintf(keynam, KEY_SIZE, "%s%s_%s", MEMCACHE_PRFX, key, postfix);

>                 "%s%s_%s",
>                 MEMCACHE_PRFX,
>                 key,
>                 postfix);
> 
>         return keyname;
> }
> 
> I have allocated memory for the prefix name
> 
> char *memcache_gstr(char *key, int id){
>         char *ret;
>         char *keyname;
>         char keyid[4];

please add:

memset(keyid, 0, sizeof(keyid));

you really need to clean out memory. There is *no* guarantees the
memory
doesn't contain values already.

>         sprintf(keyid,"%d",id);

snprintf(keyid, 4, "%d", id);

but 4 seems rather smallish. Also, why map ints to chars here? Might
as
well defer that to the snprintf call in construct_keyname, changing
the
'postfix' argument to an int.
>         keyname= construct_keyname(key,keyid);
>         printf("key: %sn", keyname);
>         ret = getmc(keyname);
>         free(keyname);
>         return ret;
> }
> then I call it here and since the return value of the keyname
> constructor is allocated memory can I free it with the pointer that
I
> assigned to it? ie: keyname
> 
> keyname is a pointer that points to the return value of
> construct_keyname() and the return value is allocated on the "heap".
Is
> that right?

Yes. Though you always have to make sure the pointer wasn't modified.
Consider the following leaking example:

char *longstring = g_new0(char, 65535);
longstring += 100; // now longstring points to the 100th
                   // char in the array
g_free(longstring); // the first 100 chars are *not* freed.
One way this is commonly solved:

char *longstring = g_new0(char, 65535);
char *start = longstring;
g_free(start);

Leakage solved!
So there is another reason to do static builds: you can use valgrind
to
help you catch this kind of problem early on.

Also: valgrind is much better than gdb or gcc to help pin-point the
cause of segfaults.
longstring += 100;
-- 
________________________________________________________________
Paul J Stevens        pjstevns @ gmail, twitter, skype, linkedin

  * Premium Hosting Services and Web Application Consultancy *

           www.nfg.nl/i...@nfg.nl/+31.85.877.99.97
________________________________________________________________
_______________________________________________
Dbmail-dev mailing list
Dbmail-dev@dbmail.org
http://mailman.fastxs.nl/cgi-bin/mailman/listinfo/dbmail-dev
_______________________________________________
Dbmail-dev mailing list
Dbmail-dev@dbmail.org
http://mailman.fastxs.nl/cgi-bin/mailman/listinfo/dbmail-dev

Reply via email to